Last active
January 17, 2024 22:11
-
-
Save cartpauj/e9e06cb47ee41dd2d33068595ece7bad to your computer and use it in GitHub Desktop.
Google eCommerce Tracking for MemberPress Thank You Pages
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 | |
//PASTE THIS CODE IN A PLUGIN LIKE My Custom Functions (RECOMMENDED) OR IN YOUR THEME'S functions.php FILE | |
//THIS ASSUMES YOU HAVE THE GOOGLE ANALYTICS JAVASCRIPT INCLUDED ALREADY | |
function echo_ga_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) { | |
//Echo the script to the HTML <head> | |
?> | |
<script type="text/javascript"> | |
ga('require', 'ecommerce', 'ecommerce.js'); | |
ga('ecommerce:addTransaction', { | |
id: <?php echo $txn->trans_num; ?>, // Transaction ID - this is normally generated by your system. | |
affiliation: 'ReputationStacker', // 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'); | |
</script> | |
<?php | |
} | |
} | |
} | |
add_action('wp_head', 'echo_ga_tracking_script'); |
@cartpauj, Is the code for mepr-ecommerce.php up to date? I am using MemberPress and would like to use this code to track eCommerce in Google Analytics. Thanks!
@dannanelli. Did you manage to add it?
This is no longer up to date with GA4...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks