Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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/'
} );
}
},
...
});
@PaulSGray
Copy link

Hi

Does this code work for the in-plugin upgrade check out or the checkout based on the developers site?

Where would I add the code please?

Thank you.

Paul

@bradvin
Copy link
Author

bradvin commented Mar 3, 2020

Does this code work for the in-plugin upgrade check out or the checkout based on the developers site?

This works on the developers site only.

@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