Skip to content

Instantly share code, notes, and snippets.

@framingeinstein
Created January 18, 2017 21:47
Show Gist options
  • Save framingeinstein/0f1847cb9614019add0b7ab4346cad3e to your computer and use it in GitHub Desktop.
Save framingeinstein/0f1847cb9614019add0b7ab4346cad3e to your computer and use it in GitHub Desktop.
Adobe Analytics UTILS
/**
* Utility function that can be used across pages... not AA dependent
*/
var UTILS = 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.dealerId = prod[0];
if(prod.length >= 2)
result.listingId = prod[1];
if(prod.length >= 3)
result.quantity = prod[2];
if(prod.length >= 4)
result.total = 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.dealerId ? product.dealerId : "");
items.push(product.listingId ? product.listingId : "");
items.push(product.quantity ? product.quantity : "");
items.push(product.total ? product.total : "");
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,
cloneArray: cloneArray
};
})($);
window.rec.UTILS = UTILS;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment