Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bradvin/012d3c882be7d85923b9390b28cb0976 to your computer and use it in GitHub Desktop.
Save bradvin/012d3c882be7d85923b9390b28cb0976 to your computer and use it in GitHub Desktop.
Tracking purchases with Google Analytics and Facebook for Freemius Checkout
//
// Gist was kindly contributed by @jamesckemp from @iconicwp. Thanks!
//
handler.open({
...
purchaseCompleted: function( response ) {
var trial = response.purchase.trial_ends !== null,
total = trial ? 0 : response.purchase.initial_amount.toString(),
productName = 'FooGallery',
storeUrl = 'https://foo.gallery/',
storeName = 'FooGallery';
if ( typeof fbq !== "undefined" ) {
fbq( 'track', 'Purchase', { currency: 'USD', value: response.purchase.initial_amount } );
}
if ( typeof ga !== "undefined" ) {
ga( 'send', 'event', 'plugin', 'purchase', productName, response.purchase.initial_amount.toString() );
ga( 'require', 'ecommerce' );
ga( 'ecommerce:addTransaction', {
'id': response.purchase.id.toString(), // Transaction ID. Required.
'affiliation': storeName, // Affiliation or store name.
'revenue': total, // Grand Total.
'shipping': '0', // Shipping.
'tax': '0' // Tax.
} );
ga( 'ecommerce:addItem', {
'id': response.purchase.id.toString(), // Transaction ID. Required.
'name': productName, // Product name. Required.
'sku': response.purchase.plan_id.toString(), // SKU/code.
'category': 'Plugin', // Category or variation.
'price': response.purchase.initial_amount.toString(), // Unit price.
'quantity': '1' // Quantity.
} );
ga( 'ecommerce:send' );
ga( 'send', {
hitType: 'pageview',
page: '/purchase-completed/',
location: storeUrl + '/purchase-completed/'
} );
}
},
...
});
@DeoThemes
Copy link

Does this code works with GTM as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment