Created
October 30, 2015 16:51
-
-
Save adardesign/38d8fbd1a831ff5fc0e8 to your computer and use it in GitHub Desktop.
adorama enhanced ecommerce analytics
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
adrma.analytics.ec = { | |
getList: function getList(sku) { | |
var findList = function findList(url) { | |
var urlParams = adrma.pageInfo.urlParams, | |
href = url ? url : window.location.href, | |
// return the value as early as possible | |
pageInfo = adrma.pageInfo, | |
listValue; | |
if (urlParams.RRref) { | |
//value RRref rich relevance (set on rich relevance zones. values are "rr cat page" "rr list page" "rr pdp" & "rr receipt page") | |
listValue = "rr"; | |
switch (pageInfo.pageType) { | |
case "productPage": | |
listValue = "rr pdp"; | |
break; | |
case "checkoutPage": | |
listValue = "rr checkout"; | |
break; | |
case "listPage": | |
listValue = "rr list page"; | |
break; | |
} | |
return listValue; | |
} | |
var accessoryList = adrma.analytics.ec.getAccessoryList(sku); | |
if (accessoryList) { | |
return accessoryList; | |
} | |
if (urlParams.searchinfo || urlParams.term) { | |
//search results (set when URL contains param "sarchinfo" or "term") | |
return "search results"; | |
} | |
if (href.indexOf("/l/") !== -1) { | |
//list page (set when URL contains "/l/") | |
return "list page"; | |
} | |
if (href.indexOf("/specials") !== -1) { | |
//specials (set when URL contains "/specials") | |
return "specials"; | |
} | |
if (urlParams.sel) { | |
listValue = ""; | |
//open box (set when URL contains param "sel=Condition_OB|ItemCondition_UsedItems") | |
//refurbished (set when URL contains param "sel=FilterBy_refurbished") | |
//used clearance (set when URL contains param "sel=Condition_F_X|ItemCondition_UsedItems") | |
//brands (set when URL contains param "sel=Brand") | |
var selParamLowerCase = urlParams.sel.toLowerCase(); | |
if (selParamLowerCase === "condition_ob" || selParamLowerCase === "itemcondition_useditems") { | |
listValue = "open box"; | |
} | |
if (selParamLowerCase === "filterby_refurbished") { | |
listValue = "refurbished"; | |
} | |
if (selParamLowerCase === "condition_f_x" || selParamLowerCase === "itemcondition_useditems") { | |
listValue = "clearance"; | |
} | |
if (selParamLowerCase === "brand") { | |
listValue = "brands"; | |
} | |
return listValue; | |
} | |
if (urlParams.promotype && urlParams.promotype === "overstock") { | |
//overstock (set when URL contains param "promotype=overstock") | |
return "overstock"; | |
} | |
if (href.indexOf("/rebates") !== -1) { | |
//rebates(set when URL contains "/rebates") | |
return "rebates"; | |
} | |
if (href.indexOf("/MyAccount#wishlist") !== -1) { | |
//wishlist (set when URL conains "adorama.com/Als.Mvc/nspc/MyAccount#wishlist") | |
return "wishlist"; | |
} | |
if (href.indexOf("/results/vip") !== -1) { | |
//vip (set when URL contains "adorama.com/results/vip") | |
return "vip"; | |
} | |
if (href.indexOf("/results") != -1) { | |
var promoVal = href.slice(href.indexOf("/results") + 9, href.length); | |
//promos (set when URL contains "/results/". List value should be promo- {name of promo} | |
return "promo-" + promoVal; | |
} | |
}; | |
// > try first the current url, | |
// > then the document.reffered | |
var list = findList(window.location.href); | |
if (!list && document.referrer) { | |
list = findList(document.referrer); | |
} | |
return list; | |
}, | |
getAccessoryList: function getAccessoryList(sku) { | |
if (!sku) return ""; | |
if ($(".addToCartPopup .RR-widget").find("[data-sku='" + sku + "']").length) { | |
return "accessories minicart"; | |
} | |
if ($(".accessoriesContainer").find("[data-sku='" + sku + "']").length) { | |
return "accessories pdp"; | |
} | |
return ""; | |
}, | |
getBundle: function getBundle(productData) { | |
var result = "not bundle"; | |
if (productData.isBundle) { | |
result = "bundle"; | |
} | |
// determine if this item is a bundle, if we dont get the data via productData (or addToCart response data) | |
return result; | |
}, | |
hasRRTitle: function getRRType() { | |
return false; // try to get the RR campain title, Need to investigate, perhaps passed in via RRTmpl as title | |
}, | |
isLoggedIn: function isLoggedIn() { | |
var result = "not logged in"; | |
if (adrma.session.session.isLoggedIn) { | |
result = "logged in"; | |
} | |
return result; | |
}, | |
isVip: function isVip() { | |
var result = ""; | |
if (adrma.session.session.isLoggedIn && adrma.session.session.isVip) { | |
result = "vip"; | |
} | |
return result; | |
}, | |
getPrice: function getPrice(productData) { | |
var price = ""; | |
if (productData.cartPrice) { | |
price = productData.cartPrice; | |
return price; | |
} | |
if (productData.price) { | |
price = price; | |
return price; | |
} | |
if (productData.prices && productData.prices.price) { | |
price = productData.prices.price; | |
return price; | |
} | |
}, | |
getStock: function getStock(productData) { | |
var stock = ""; | |
if (productData.stock) { | |
if (productData.stock.indexOf("Stock") !== -1) { | |
stock = productData.stock; | |
} else { | |
stock = productData.stock + "Stock"; | |
} | |
} | |
return stock; | |
}, | |
getBadges: function getBadges(productData) { | |
if (productData.priceBadges) { | |
return productData.priceBadges; | |
} | |
return productData.badges ? productData.badges.join(" ") : ""; | |
}, | |
collectDataObj: function collectDataObj(productData) { | |
var self = this; | |
var dataObj = { | |
id: productData.sku ? productData.sku : productData.id, | |
name: productData.title.slice(0, 50), | |
category: productData.category, // need to get the catagory via the product or via page. | |
brand: productData.brand ? productData.brand : productData.title.split(" ")[0], // need to get reliably from productData but untill then, grab first word of tittle | |
//productData.prices.price | |
price: self.getPrice(productData), // Product price (currency). | |
quantity: productData.quantity ? productData.quantity : 1, | |
dimension4: self.getStock(productData), // assuming that the data will have a stock vlaue and it will be "In" "out" "Low" | |
dimension6: self.isLoggedIn(), | |
dimension7: self.isVip(), | |
dimension8: self.getBundle(productData), | |
dimension9: self.getBadges(productData), // Product-scoped custom dimension (string) used to track price badges. | |
dimension10: productData.priceType ? productData.priceType : "", | |
dimension11: productData.itemCondition ? productData.itemCondition : "" // Product-scoped custom dimension (string) used to track item condition. | |
}; | |
return dataObj; | |
}, | |
addProducts: function addProduct(products) { | |
$.each(products, function iterateProductEC(i, e) { | |
ga("newTracker.ec:addProduct", e); | |
}); | |
}, | |
send: function sendAddProduct(action, sku) { | |
self = this; | |
var actionObj = {}, | |
list = self.getList(sku); | |
if (list) { | |
actionObj.list = list; // Product list (string). Pull value from url param promo. | |
} | |
ga('newTracker.ec:setAction', action, actionObj); | |
ga('newTracker.send', 'event', "ec", action); | |
} | |
}; | |
adrma.session.getSession.done(function gotSession(session) { | |
var self = adrma.analytics.ec, | |
pageInfo = adrma.pageInfo; | |
if (!adrma.siteSettings.EnableEnhancedECommerce) { | |
return; | |
} | |
if (!ga) { | |
return; | |
} | |
ga("create", adrma.siteSettings.GoogleAnalyticsID, "auto", { | |
"name": "newTracker" | |
}); | |
ga("newTracker.require", "ec"); | |
switch (pageInfo.pageType) { | |
case "productPage": | |
var productData = adrma.productData[adrma.pageInfo.id], | |
products = self.collectDataObj(productData), | |
actionObj = {}, | |
list = self.getList(); | |
self.addProducts([products]); | |
if (list) { | |
actionObj.list = list; // Product list (string). Pull value from url param promo. | |
} | |
ga("newTracker.ec:setAction", "detail", actionObj); | |
ga("newTracker.send", "pageview"); | |
break; | |
case "checkoutPage": | |
break; | |
case "receipt": | |
//TODO implement | |
break; | |
} | |
$.subscribe("addToCart", function addToCartECLog(e, data) { | |
var products = [], | |
firstSku; | |
for (var i = 0; i < data.processedItems.length; i++) { | |
products.push(self.collectDataObj(data.processedItems[i])); | |
} | |
firstSku = products[0].sku; // pass along just the first SKU | |
self.addProducts(products); | |
self.send("add", firstSku); | |
}); | |
$.subscribe("removeFromCart", function addToCartECLog(e, data) { | |
var products = []; | |
if (!data || !data.items || !data.items.length) { | |
return; | |
} | |
for (var i = 0; i < data.items.length; i++) { | |
products.push(self.collectDataObj(data.items[i])); | |
} | |
self.addProducts(products); | |
self.send("remove"); | |
}); | |
$.subscribe("gotData/GetCheckoutSummary", function addToCartECLog(e, data) { | |
var items = data.data.items, | |
products = [], | |
theItem, | |
item; | |
for (item in items) { | |
theItem = items[item]; | |
product = self.collectDataObj(adrma.productData[items[item].sku]); | |
product.price = theItem.unitPrice; | |
product.quantity = theItem.qty; | |
products.push(product); | |
if (theItem['package'] && theItem['package'].length) { | |
var i = 0, | |
packages = theItem['package'], | |
packageLength = packages.length, | |
pack; | |
for (; i < packageLength; i++) { | |
pack = packages[i]; | |
product = self.collectDataObj(adrma.productData[pack.sku]); | |
product.price = pack.unitPrice; | |
product.quantity = pack.qty; | |
products.push(product); | |
} | |
} | |
self.addProducts(products); | |
ga("newTracker.ec:setAction", "checkout"); | |
ga("newTracker.send", "pageview"); | |
} | |
}); | |
}); |
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
(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', '//www.google-analytics.com/analytics.js', 'ga'); | |
ga('create', adrma.siteSettings.GoogleAnalyticsID, 'auto'); | |
ga('send', 'pageview'); | |
adrma.analytics = { | |
init: function() { | |
// moved the GA snippet to the page itself - WEB-27337 | |
$.publish("ga/loaded"); | |
adrma.analytics.loadPlugins(); | |
this.bindActions(); | |
}, | |
types: { | |
pageView: "pageview", | |
trackEvent: "event", | |
setCustomVar: "_setCustomVar" | |
}, | |
add: function add(options) { | |
var self = this; | |
if (!options.type) { | |
return; | |
} | |
var arr = {}; | |
options = $.each(options, function(e, i) { // filter string null's | |
if (options[e] === "null") { | |
options[e] = null; | |
} | |
}); | |
switch (options.type) { | |
case "pageview": | |
self.addPageView(options); | |
break; | |
case "event": | |
self.addEvent(options); | |
} | |
}, | |
plugins: [], | |
loadPlugins: function loadPlugins() { | |
$.each(this.plugins, function plugin(i, e) { | |
if ($.isFunction(e)) { | |
e(); | |
} | |
}); | |
}, | |
addPageView: function addPageView(options) { | |
if (!window.ga) { | |
return; | |
} | |
var pageView = {}; | |
if (options.path) { | |
pageView.page = options.path | |
} | |
if (options.title) { | |
pageView.title = options.title | |
} | |
if (JSON.stringify(pageView) != '{}') { | |
ga('send', 'pageview', pageView); | |
} else { | |
ga('send', 'pageview'); | |
} | |
}, | |
addEvent: function addEvent(options) { | |
//based on ga('send','event','Category','Action','Label', Value, opt_noninteraction) | |
var event = { | |
'hitType': 'event' | |
}, | |
interval, | |
self = this, | |
ops; | |
if (!window.ga) { | |
ops = options; | |
interval = setInterval(function gaNotDefined() { | |
if (window.ga) { | |
clearInterval(interval); | |
self.addEvent(ops); | |
} | |
}, 100); | |
return; | |
} | |
if (options.category) { | |
event.eventCategory = options.category; | |
} | |
if (options.action) { | |
event.eventAction = options.action; | |
} | |
if (options.label) { | |
event.eventLabel = options.label; | |
} | |
if (options.value !== undefined) { | |
event.eventValue = options.value; | |
} | |
//opt_noninteraction | |
if (options.noninteraction) { | |
event.nonInteraction = 1 | |
} | |
ga('send', event); | |
}, | |
setDimension: function addCustomVar(options) { | |
ga("send", "dimension" + options.index, options.value); | |
}, | |
// attaches GA event tracking - NEWWEB-7889 | |
trackEvents: function trackEvents(ele) { | |
var self = this; | |
var jThis = $(ele), | |
data = jThis.attr("data-trackData") || jThis.attr("data-track-data"); // added data-track-data for compatibilty where generated | |
if (!data) return true; | |
data = data.split(","); | |
self.sendEventData(data); | |
}, | |
bindActions: function bindActions() { | |
var self = this; | |
$(document).on("click", ".trackEvent", function(e) { | |
self.trackEvents($(this)); | |
}); | |
$.subscribe("tooltip/shown", function(e, triggerEle) { | |
if ($(triggerEle).attr("data-track-data")) { | |
self.trackEvents(triggerEle); | |
} | |
}); | |
}, | |
trackView: function trackView(collection) { | |
var self = this, | |
data, | |
delay = 1000, | |
ele; | |
collection = collection ? $(collection) : $("body"); | |
collection.find("[data-track-view]").each(function(i, e) { | |
delay = delay + 1001; | |
ele = $(e); | |
data = ele.attr("data-track-view"); | |
ele.remove(); | |
if (data) { | |
(function(data) { | |
setTimeout(function() { | |
var arrayData = data.split(","); | |
arrayData[4] = true; | |
self.sendEventData(arrayData); | |
}, delay); | |
})(data); | |
} | |
}); | |
}, | |
//based on ga('send','event','Category','Action','Label', Value, opt_noninteraction) | |
sendEventData: function sendEventData(data) { | |
if (!data[3]) { | |
data[3] = 0; | |
} | |
data[4] = data[4] || false; | |
adrma.analytics.add({ | |
type: "event", | |
category: data[0], | |
action: data[1], | |
label: data[2], | |
value: data[3], | |
noninteraction: data[4] | |
}); | |
} | |
}; | |
adrma.init.add({ | |
name: "GA", | |
cb: function() { | |
adrma.analytics.init(); | |
adrma.analytics.trackEvents(); | |
}, | |
defer: true | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment