Skip to content

Instantly share code, notes, and snippets.

@alewolf
Last active August 12, 2021 06:29
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 alewolf/ef43dfe3de282c56feb83ee1a1478532 to your computer and use it in GitHub Desktop.
Save alewolf/ef43dfe3de282c56feb83ee1a1478532 to your computer and use it in GitHub Desktop.
// First you'll need to load the gtag.js library
function load_library(){
echo '<script async src="https://www.googletagmanager.com/gtag/js?id=GA_UA_PROPERTY_ID"></script>';
}
// Then output the conversion tracking scripts
// The Google Ads conversion ID must include the "AW-" in front of the number
function (response)
{
let
isTrial = (null != response.purchase.trial_ends),
isSubscription = (null != response.purchase.initial_amount),
total = isTrial ? 0 : (isSubscription ? response.purchase.initial_amount : response.purchase.gross).toString(),
productName = 'PLUGIN_NAME',
storeName = 'PLUGIN_STORE_NAME';
googleAdsConversionId = 'GOOGLE_ADS_CONVERSION_ID';
googleAdsConversionLabel = 'GOOGLE_ADS_CONVERSION_LABEL';
googleAnalyticsUniversalPropertyId = 'GA_UA_PROPERTY_ID';
googleAnalytics4MeasurementId = 'GA4_MEASUREMENT_ID';
facebookPixelId = 'FACEBOOK_PIXEL_ID';
window.dataLayer = window.dataLayer || [];
function gtag()
{
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', googleAnalyticsUniversalPropertyId, { 'anonymize_ip': 'true' });
gtag('config', googleAnalytics4MeasurementId);
gtag('config', googleAdsConversionId);
gtag('event', 'purchase', {
'send_to': [googleAnalyticsUniversalPropertyId, googleAnalytics4MeasurementId],
'transaction_id': response.purchase.id.toString(),
'currency': 'USD',
'discount': 0,
'items': [{
'id': response.purchase.plan_id.toString(),
'quantity': 1,
'price': total,
'name': productName,
'category': 'Plugin',
}],
'affiliation': storeName,
'value': response.purchase.initial_amount.toString()
});
gtag('event', 'conversion', {
'send_to': googleAdsConversionId + '/' + googleAdsConversionLabel,
'value': response.purchase.initial_amount.toString(),
'currency': 'USD',
'transaction_id': response.purchase.id.toString()
});
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', facebookPixelId);
fbq('track', 'PageView');
fbq('track', 'Purchase', {
currency: 'USD',
value: total
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment