Skip to content

Instantly share code, notes, and snippets.

@framingeinstein
Created January 19, 2017 15:58
Show Gist options
  • Save framingeinstein/02e67e40d47d7dbc9a6b3772596428b0 to your computer and use it in GitHub Desktop.
Save framingeinstein/02e67e40d47d7dbc9a6b3772596428b0 to your computer and use it in GitHub Desktop.
Initialize Data Layer Helper for working with GTM style data layers with adobe analytics / DTM
window.ana = window.ana || {};
/**
* Utility function that can be used across pages... not AA dependent
*/
var UTILS = window.ana.UTILS || ( function( $ ) {
function cloneArray( array) {
return array.slice(0);
}
function parseProductString ( products ) {
var prods = products.split(',');
var results = [];
prods.forEach ( function( product ) {
var prod = product.split(';');
var result = {};
result.name = prod[0];
if(prod.length >= 2)
result.id = prod[1];
if(prod.length >= 3)
result.quantity = prod[2];
if(prod.length >= 4)
result.price = prod[3];
if(prod.length >= 5)
result.events = prod[4].split('|');
if(prod.length >= 6)
result.merchandizing = prod[5].split('|');
}, this);
}
function getProductString ( products ) {
var result = [];
products.forEach ( function( product ) {
var items = [];
items.push(product.name ? product.name : "");
items.push(product.id ? product.id : "");
items.push(product.quantity ? product.quantity : "");
items.push(product.price ? product.price : "");
items.push(product.events ? (Array.isArray(product.events) ? product.events.join("|"):product.events) : "");
items.push(product.merchandizing ? (Array.isArray(product.merchandizing) ? product.merchandizing.join("|"):product.merchandizing) : "");
result.push(items.join(';'));
}, this );
return result.join(',');
}
function trigger(target, event, detail){
var element = $(target);
if(element.length == 0){
console.error('Invalid Target');
return;
}
element = element.get(0);
detail = detail || {};
// First create the event
var dtmEvent = new CustomEvent(event, {
detail: detail
});
// Trigger it!
element.dispatchEvent(dtmEvent);
}
return {
getProductString: getProductString,
trigger: trigger
};
})(jQuery);
window.ana.UTILS = UTILS;
(function(){/*
jQuery v1.9.1 (c) 2005, 2012
jQuery Foundation, Inc. jquery.org/license.
*/
var g=/\[object (Boolean|Number|String|Function|Array|Date|RegExp)\]/;function h(a){return null==a?String(a):(a=g.exec(Object.prototype.toString.call(Object(a))))?a[1].toLowerCase():"object"}function k(a,b){return Object.prototype.hasOwnProperty.call(Object(a),b)}function m(a){if(!a||"object"!=h(a)||a.nodeType||a==a.window)return!1;try{if(a.constructor&&!k(a,"constructor")&&!k(a.constructor.prototype,"isPrototypeOf"))return!1}catch(b){return!1}for(var c in a);return void 0===c||k(a,c)};/*
Copyright 2012 Google Inc. All rights reserved. */
function n(a,b,c){this.b=a;this.f=b||function(){};this.d=!1;this.a={};this.c=[];this.e=p(this);r(this,a,!c);var d=a.push,e=this;a.push=function(){var b=[].slice.call(arguments,0),c=d.apply(a,b);r(e,b);return c}}window.DataLayerHelper=n;n.prototype.get=function(a){var b=this.a;a=a.split(".");for(var c=0;c<a.length;c++){if(void 0===b[a[c]])return;b=b[a[c]]}return b};n.prototype.flatten=function(){this.b.splice(0,this.b.length);this.b[0]={};s(this.a,this.b[0])};
function r(a,b,c){for(a.c.push.apply(a.c,b);!1===a.d&&0<a.c.length;){b=a.c.shift();if("array"==h(b))a:{var d=b,e=a.a;if("string"==h(d[0])){for(var f=d[0].split("."),u=f.pop(),d=d.slice(1),l=0;l<f.length;l++){if(void 0===e[f[l]])break a;e=e[f[l]]}try{e[u].apply(e,d)}catch(v){}}}else if("function"==typeof b)try{b.call(a.e)}catch(w){}else if(m(b))for(var q in b)s(t(q,b[q]),a.a);else continue;c||(a.d=!0,a.f(a.a,b),a.d=!1)}}
function p(a){return{set:function(b,c){s(t(b,c),a.a)},get:function(b){return a.get(b)}}}function t(a,b){for(var c={},d=c,e=a.split("."),f=0;f<e.length-1;f++)d=d[e[f]]={};d[e[e.length-1]]=b;return c}function s(a,b){for(var c in a)if(k(a,c)){var d=a[c];"array"==h(d)?("array"==h(b[c])||(b[c]=[]),s(d,b[c])):m(d)?(m(b[c])||(b[c]={}),s(d,b[c])):b[c]=d}};})();
function listener(model, message){
window.ana.model = model; // this is the abstrack model... despite the bad naming
console.log(message, model);
if(window.ana.model.event){
window.ana.UTILS.trigger('body', window.ana.model.event);
}
}
window.ana.helper = new DataLayerHelper(dataLayer, listener, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment