Last active
October 4, 2019 12:51
-
-
Save geeac/37e6b0b74edb462ea2de6f07d920ec18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add Google Analytics eCommerce tracking for MemberPress | |
function echo_ga_tracking_script() { | |
global $wpdb; | |
if(isset($_GET['membership']) && isset($_GET['trans_num'])) { | |
// get transaction info for one time payments | |
$txn = MeprTransaction::get_one_by_trans_num($_GET['trans_num']); | |
// check if it has subscription info attached | |
if(isset($_GET['subscr_id']) && ($_GET['subscr_id'] == $_GET['trans_num'])) { | |
// Get subscription id from the subscr_id | |
$sid = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}mepr_subscriptions WHERE subscr_id LIKE '{$_GET['subscr_id']}'" ); | |
// get array of transactions for recurring payments | |
//$txn = MeprTransaction::get_one_by_subscription_id($sid); | |
$txns = MeprTransaction::get_all_by_subscription_id($sid); | |
// For Stripe CC, 2 transactions are added: txn_type = subscription_confirmation and txn_type = payment | |
// We want the 2nd one that holds the payment amount | |
// For paypal express there is one transaction with txn_type = subscription_confirmation that gets updated after the first load of the thank-you page | |
// with payment info, so we need to do a page reload in that case | |
if ($txns[0]->txn_type == 'payment') { | |
$txn = $txns[0]; | |
} | |
else if ( ($txns[0]->txn_type == 'subscription_confirmation') && isset($txns[1]) && ($txns[1]->txn_type == 'payment') ) { | |
$txn = $txns[1]; | |
} | |
else { // reload thank-you page to get the updated transaction info for Paypal payments | |
echo '<script>setTimeout((function() { window.location.reload(true); }), 5000);'; | |
echo 'document.getElementById("et-boc").innerHTML = "<div style=\"padding:10%;text-align:center;font-size:2em;\">Please keep this window open until we finish processing your payment...</div>";</script>'; | |
} | |
} // end isset $_GET['subscr_id'] | |
} // end isset($_GET['membership'] | |
?> | |
<script> | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-xxxx-x', 'auto'); | |
<?php | |
if(isset($txn->id) && $txn->id > 0 && ($txn->txn_type == 'payment')) { | |
//var_dump($txn); | |
//Echo the script | |
?> | |
ga('require', 'ecommerce'); | |
ga('ecommerce:addTransaction', { | |
id: '<?php echo $txn->trans_num; ?>', // Transaction ID - this is normally generated by your system. | |
affiliation: 'XXXXXXXXXXX', // Affiliation or store name | |
revenue: '<?php echo $txn->total; ?>', // Grand Total | |
shipping: '0', // Shipping cost | |
tax: '<?php echo $txn->tax_amount; ?>' | |
}); // Tax. | |
ga('ecommerce:addItem', { | |
id: '<?php echo $txn->trans_num; ?>', // Transaction ID. | |
sku: '', // SKU/code. | |
name: '<?php echo urldecode($_GET['membership']); ?>', // Product name. | |
category: '', // Category or variation. | |
price: '<?php echo $txn->amount; ?>', // Unit price. | |
quantity: '1' | |
}); | |
ga('ecommerce:send'); | |
<?php | |
} // end isset txn | |
?> | |
ga('send', 'pageview'); | |
</script> | |
<?php | |
//echo "<!-- ecom tracking "; | |
//var_dump($txn); | |
//var_dump($txns); | |
//echo "-->"; | |
} | |
add_action('wp_footer', 'echo_ga_tracking_script', 20); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment