Last active
January 23, 2019 01:59
-
-
Save anthonybaldwin/7792c8208063420e48b4f2e9e1072cbc to your computer and use it in GitHub Desktop.
Send ONTRAPORT Order Form Transaction Information to Google Analytics/Ecommerce
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
NOTE: | |
Please test the setup before launching! This has not been tested alongside Google Tag Manager (GTM), | |
or any other Google Analytics setup. However, this has been confirmed to pass transaction and products, | |
tying them to visitor or GA Client ID. (e.g., using THB currency: https://goo.gl/QDjAjq) | |
INSTRUCTIONS: | |
1. Enable Google Analytics (GA) / Ecommerce and obtain GA ID: https://goo.gl/qQUifv | |
2. Copy the minified version of the script (below), or: https://goo.gl/cvcEVk | |
(you can also view / edit the readable version as needed, also below, or: https://goo.gl/Zzm2ey) | |
3. Add your GA ID to the top of the script, along w/ choice of currency transactions will be sent in | |
(you can also change GA currency globally, but I still recommend filling in the variable in the script as well. | |
More info here: https://goo.gl/Ms8RkV and: https://goo.gl/hp96hg) | |
4. Enable GA Ecommerce on your Order Form, and place the edited script in the header of your thank you page | |
NOTE ABOUT PAGE VISITS: | |
This is designed to send a visit of the TY page along with the purchase information. For Google Analytics to assume | |
this contact is not starting their visit/session and purchasing at the same time, you need to be sure to set up GA | |
on your other pages. (More info: https://goo.gl/cKh23a) |
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
<script> | |
<!---- CONFIGURATION FOR GOOGLE ANALYTICS ECOMMERCE -----> | |
var googleAnalyticsId = "UA-123456789-1", // replace with your Google Analytics ID | |
googleAnalyticsCurrency = "USD"; // currency you wish to send purchases in | |
<!---- DO NOT EDIT BELOW THIS LINE -----> | |
function gv(e){for(var a=window.location.search.substring(1).split("&"),r=0;r<a.length;r++){var t=a[r].split("=");if(t[0]==e)return decodeURIComponent(t[1]).replace(/\+/g," ")}return!1}function gae(){var e=gv("OrderId");if(e){ga("require","ecommerce");var a=gv("Revenue"),r=gv("Shipping"),t=gv("Tax");ga("ecommerce:addTransaction",{id:e,revenue:a,shipping:r,tax:t,currency:googleAnalyticsCurrency});var i=1,n=1;do{var c=gv("Item"+n+"Id"),o=gv("Item"+n+"Name"),g=c||o;if(c||o){var u=gv("Item"+n+"Price"),l=gv("Item"+n+"Quantity");ga("ecommerce:addItem",{id:e,name:o,price:u,quantity:l,sku:g}),n++}else i=0}while(i);ga("ecommerce:send"),ga("ecommerce:clear")}return!1}!function(e,a,r,t,i,n,c){e.GoogleAnalyticsObject=i,e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},e[i].l=1*new Date,n=a.createElement(r),c=a.getElementsByTagName(r)[0],n.async=1,n.src="https://www.google-analytics.com/analytics.js",c.parentNode.insertBefore(n,c)}(window,document,"script",0,"ga"),ga("create",googleAnalyticsId,"auto"),ga("send","pageview"),gae(); | |
</script> |
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
<script> | |
<!---- CONFIGURATION FOR GOOGLE ANALYTICS ECOMMERCE -----> | |
var googleAnalyticsId = "UA-123456789-1", // replace with your Google Analytics ID | |
googleAnalyticsCurrency = "USD"; // currency you wish to send purchases in | |
/* | |
Tip: | |
you can see what is being sent to Google by using this Chrome plugin and the browser console: https://goo.gl/fmijYN | |
example screenshot: https://goo.gl/YNNbK9 | |
*/ | |
<!---- DO NOT EDIT BELOW THIS LINE -----> | |
// getQueryVariable() taken from: https://goo.gl/XzJzic | |
function getQueryVariable(variable) | |
{ | |
var query = window.location.search.substring(1); | |
var vars = query.split("&"); | |
for (var i=0;i<vars.length;i++) { | |
var pair = vars[i].split("="); | |
if(pair[0] == variable){return decodeURIComponent(pair[1]).replace(/\+/g, ' ');} | |
} | |
return(false); | |
} | |
// default anayltics/ecommerce stuff from: https://goo.gl/WF7HmK and https://goo.gl/K7vzAe | |
// default google analytics js | |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
// intiate analytics | |
ga('create', googleAnalyticsId, 'auto'); | |
//send page view | |
ga('send', 'pageview'); | |
// doPurchases() made to bake the cake: https://goo.gl/QAJaJf | |
function doPurchases() | |
{ | |
var transactionId = getQueryVariable('OrderId'); | |
if (transactionId) // if OrderId exists in URL | |
{ | |
// load ecommerce plugin, and only if we need to | |
ga('require', 'ecommerce'); | |
var revenue = getQueryVariable('Revenue'), | |
shipping = getQueryVariable('Shipping'), | |
tax = getQueryVariable('Tax'); | |
// create transaction | |
ga('ecommerce:addTransaction', { | |
'id': transactionId, | |
'revenue': revenue, | |
'shipping': shipping, | |
'tax': tax, | |
'currency': googleAnalyticsCurrency | |
}); | |
var moreProducts = 1, | |
i = 1; | |
// create purchase request | |
do { | |
var productId= getQueryVariable('Item'+i+'Id'), | |
productName = getQueryVariable('Item'+i+'Name'), | |
productSku = (productId) ? productId : productName; | |
// if there's a Product Id or name it means there's a purchase | |
if (productId || productName) | |
{ | |
var productPrice = getQueryVariable('Item'+i+'Price'), | |
productQuantity = getQueryVariable('Item'+i+'Quantity'); | |
// product purchase | |
ga('ecommerce:addItem', { | |
'id': transactionId, | |
'name': productName, | |
'price': productPrice, | |
'quantity': productQuantity, | |
'sku': productSku | |
}); | |
i++; | |
} | |
else // no more products, stop | |
{ | |
moreProducts = 0; | |
} | |
} | |
while (moreProducts) | |
ga('ecommerce:send'); // send transaction/purchases | |
ga('ecommerce:clear'); // clear the cart | |
} | |
return(false); | |
} | |
doPurchases(); // do it | |
</script> |
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
EXAMPLE: | |
doPurchases() takes this URL data from ONTRAPORT: | |
https://mythankyoupage.com? | |
OrderId=1234 | |
&Revenue=8.06 | |
&Shipping=1.01 | |
&Tax=0.99 | |
&Item1Name=MyFancyProduct1 | |
&Item1Price=1.01 | |
&Item1Quantity=1 | |
&Item1Id=1 | |
&Item2Name=AnotherFancyProduct2 | |
&Item2Price=2.02 | |
&Item2Quantity=1 | |
&Item2Id=2 | |
&Item3Name=LastFancyProduct3 | |
&Item3Price=3.03 | |
&Item3Quantity=1 | |
&Item3Id=3 | |
And builds/sends the below Google Analytics request: | |
a('ecommerce:addTransaction', { | |
'id': '1234', | |
'revenue': '8.06', | |
'shipping': '1.01', | |
'tax': '0.99', | |
'currency': 'USD' | |
}); | |
ga('ecommerce:addItem', { | |
'id': '1234', | |
'name': 'MyFancyProduct1', | |
'price': '1.01', | |
'quantity': '1', | |
'sku': '1' | |
}); | |
ga('ecommerce:addItem', { | |
'id': '1234', | |
'name': 'AnotherFancyProduct2', | |
'price': '2.02', | |
'quantity': '1', | |
'sku': '2' | |
}); | |
ga('ecommerce:addItem', { | |
'id': '1234', | |
'name': 'LastFancyProduct3', | |
'price': '3.03', | |
'quantity': '1', | |
'sku': '3' | |
}); | |
ga('ecommerce:send'); | |
ga('ecommerce:clear'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question, can this be adapted for FB pixel?