Last active
April 6, 2023 21:58
-
-
Save vovafeldman/91203e0383e724301008f9e12d0264cb 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 = '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/' | |
} ); | |
} | |
}, | |
... | |
}); |
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
@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
Gist was kindly contributed by @jamesckemp from @iconicwp. Thanks!