Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.