Forked from vovafeldman/freemius-checkout-ga-fb-tracking.js
Last active
August 18, 2020 10:53
-
-
Save bradvin/012d3c882be7d85923b9390b28cb0976 to your computer and use it in GitHub Desktop.
Tracking purchases with Google Analytics and Facebook for Freemius Checkout
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
// | |
// 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/' | |
} ); | |
} | |
}, | |
... | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this code works with GTM as well?