Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Created December 12, 2014 16:33
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 dalethedeveloper/460e7c72c1d1e9f55a94 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/460e7c72c1d1e9f55a94 to your computer and use it in GitHub Desktop.
Check the Google Tag Manager dataLayer for an Ecommerce transaction and do something only once (cookie enforced)
if (typeof (window.dataLayer) !== 'undefined' && window.dataLayer.length) {
for (var d = 0; d < window.dataLayer.length; d++) {
var obj = window.dataLayer[d],
keys = Object.keys(obj);
for (var k = 0; k < keys.length; k++) {
var key = keys[k],
val = obj[key];
if (key == 'ecommerce') {
var token = 'order-' + val.purchase.actionField.id;
var sent = new RegExp(token).test(document.cookie);
if (!sent) {
// Do Stuff
// Set a cookie to not run again for an hour
var d = new Date(Date.now() + 3600000);
document.cookie = token + '=sent; expires=' + d.toGMTString() + '; path=' + window.location.pathname;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment