-
-
Save klevu/3ac489ab309dd9ebd35ac0cd6c2be7ac to your computer and use it in GitHub Desktop.
RECS GA - Basic Steps
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
<script type="text/javascript"> | |
klevu.coreEvent.build({ | |
name: "gtmCheckwindowdatalayer", | |
fire: function () { | |
if (typeof window.dataLayer !== "undefined") { | |
return true; | |
} | |
return false; | |
}, | |
maxCount: 100, | |
delay: 150 | |
}); | |
klevu.coreEvent.attach("gtmCheckwindowdatalayer", { | |
name: "initRecs", | |
fire: function () { | |
(function (klevu) { | |
klevu.extend(true, klevu, { | |
ga: { | |
/** | |
* Modify this to change the appearance of your campaign in GA. | |
*/ | |
getPageTitle: function () { | |
var pageTitle = ""; | |
if (typeof klevu_page_meta !== "undefined" && klevu_page_meta.pageType) { | |
switch (klevu_page_meta.pageType) { | |
case "category": pageTitle = klevu_page_meta.categoryName; break; | |
case "pdp": pageTitle = klevu_page_meta.itemName; break; | |
case "cart": pageTitle = "Cart"; break; | |
case "home": pageTitle = "Homepage"; break; | |
default: pageTitle = "Other"; break; | |
} | |
} | |
return pageTitle; | |
} | |
} | |
}); | |
})(klevu); | |
/** | |
* Inform GA when a Recommendation banner has been Viewed. | |
*/ | |
window.klevu_processRECSTemplateData = function klevu_processRECSTemplateData(templateData, recsKey) { | |
var pageTitle = klevu.ga.getPageTitle(); | |
var currencyCode = window.klevu_prepareGaCurrency(templateData); | |
var listTitle = "klevu-" + pageTitle + "-" + templateData.metadata.title; | |
var productList = templateData.query.klevuRECSItemList.result.map(function (product, key) { | |
var productData = window.klevu_prepareProductGaData(product); | |
productData.position = key; | |
productData.list = listTitle; | |
return productData; | |
}); | |
window.dataLayer.push({ | |
event: 'klevuProductListImpression', | |
ecommerce: { | |
impression: { | |
list: listTitle | |
}, | |
currencyCode: currencyCode, | |
impressions: productList | |
}, | |
}); | |
}; | |
/** | |
* Inform GA when a product in a Recommendation banner has been Clicked. | |
*/ | |
window.klevu_RECSItemClick = function klevu_RECSItemClick(templateData, itemId, recsKey) { | |
var pageTitle = klevu.ga.getPageTitle(); | |
var listTitle = "klevu-" + pageTitle + "-" + templateData.metadata.title; | |
if (itemId != "" && recsKey) { | |
var key = 0; | |
var product = templateData.query.klevuRECSItemList.result.find(function (recommendationProduct, index) { | |
var check = (recommendationProduct.id == itemId); | |
if (check) { | |
key = index; | |
} | |
return check; | |
}); | |
var productData = window.klevu_prepareProductGaData(product); | |
productData.position = key; | |
window.dataLayer.push({ | |
'event': 'klevuProductListClick', | |
'ecommerce': { | |
'click': { | |
'actionField': { | |
'list': listTitle | |
}, | |
'products': [productData] | |
} | |
} | |
}); | |
} | |
}; | |
/** | |
* Modify these methods according to your own requirements of data to be sent to GA. | |
*/ | |
window.klevu_prepareProductGaData = function klevu_prepareProductGaData(product) { | |
return { | |
'id': product.sku, // OR product.id OR product.itemGroupId | |
'name': product.name, | |
'price': product.salePrice != undefined ? product.salePrice : product.price, | |
'category': product.category, | |
'variant': product.id // OR product.sku OR product.itemGroupId | |
}; | |
}; | |
window.klevu_prepareGaCurrency = function klevu_prepareGaCurrency(templateData) { | |
if (templateData.query.klevuRECSItemList.result.length <= 0) { | |
return ''; | |
} | |
// return templateData.query.klevuRECSItemList.result[0].currency; | |
return templateData.query.klevuRECSItemList.result[0].storeBaseCurrency; | |
}; | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment