Skip to content

Instantly share code, notes, and snippets.

@vovafeldman
Last active April 6, 2023 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vovafeldman/91203e0383e724301008f9e12d0264cb to your computer and use it in GitHub Desktop.
Save vovafeldman/91203e0383e724301008f9e12d0264cb 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 = 'Product Name',
storeUrl = 'https://your-site.com',
storeName = 'Store Name';
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/'
} );
}
},
...
});
@vovafeldman
Copy link
Author

Gist was kindly contributed by @jamesckemp from @iconicwp. Thanks!

@irfan-blackandwhite
Copy link

Hi ,
would you elaborate where we add this code in the plugin. we have Facebook pixel code in the header of plugin's pages. OR add it to other page? waiting for your reply.
Many thanks
irfan Qasim

@Tobias-Conrad
Copy link

@vovafeldman
Please where to add in plugin and how to add a custom script or a tracking pixel?
Sales tracking is very essential for good PPC marketing.

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