I am wondering how I can add a google tracking on MemberPress "Thank You" page, basically after a user had paid a monthly or recurring payment and then it will send the data to google analytics with "the name of the plan which they bought", "the payment amount" and "transaction id".
I got this code but its not triggering anything or maybe this is not the right code? I don't know how to debug MemberPress so I am having problems, anyone please help...
function echo_mepr_tracking_script() {
if(isset($_GET['membership']) && isset($_GET['trans_num'])) {
$txn = MeprTransaction::get_one_by_trans_num($_GET['trans_num']);
if(isset($txn->id) && $txn->id > 0) {
$trans_id = $txn->id;
$total = $txn->total;
// Update total if recurring
if($txn->subscription_id > 0) {
$sub = new MeprSubscription($txn->subscription_id);
if(isset($sub->id) && $sub->id > 0) {
$trans_id = $sub->id; // Use subscription ID as the payment id for recurring subscriptions
//Trial period?
if($sub->trial) {
$total = $sub->trial_amount;
} else {
$total = $sub->total;
}
}
}
?>
<script>
var fh5coMeprTxnNum = '<?php echo urlencode($trans_id); ?>';
var fh5coMeprTxnAmount = '<?php echo floatval($total); ?>';
fbq('track', 'Purchase', {currency: "USD", value: fh5coMeprTxnAmount});
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': fh5coMeprTxnNum,
'revenue': fh5coMeprTxnAmount
});
ga('ecommerce:send');
alert('yes');
</script>
<?php
}
}
}
add_action('wp_footer', 'echo_mepr_tracking_script');
Google requires some additional variables to be passed for it's ecommerce tracking.
I recently implemented this on a number of MemberPress thank you pages, based on code from this gist but using MemberPress Shortcodes.
If you prefer to implement via a Wordpress action hook then just be sure to include all of the required variables as defined by Google Analytics:
addTransaction: id, total
addItem: name, sku, price, quantity