Created
December 15, 2015 17:21
-
-
Save xymor/b228f7f390af41f14a6a to your computer and use it in GitHub Desktop.
datalayer.gsp
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
<g:js file="encodeBase64" /> | |
<g:js file="jquery.cookie" /> | |
<script type="text/javascript"> | |
var dataLayer = dataLayer || []; | |
var _data = {}; | |
var pageName = '${params.page}'; | |
var injectCustomer = function(obj){ | |
var userId64 = $.cookie('${kanlo.storeSymbol()}UserId'); | |
if(!userId64) { | |
obj.customerLoggedIn = false; | |
return | |
} | |
obj.customerLoggedIn = true; | |
obj.customerId = Base64.decode(userId64); | |
}; | |
var injectProduct, injectCart; | |
injectProduct = injectCart = function(){}; | |
<g:if test="${params.page == 'home'}"> | |
injectCustomer(_data); | |
dataLayer = [_data]; | |
</g:if> | |
<g:if test="${params.page == 'category'}"> | |
_data.categoryId = '${category.id}'; | |
_data.categoryName = '${category.name?.replaceAll("\'","\\\\\'")}'; | |
_data.categoryUri = '${category.customUri?.uri ?: category.getDefaultUri()}'; | |
</g:if> | |
<g:if test="${params.page in ['search', 'tag', 'category']}"> | |
var productList = []; | |
<g:iterateProductList> | |
productList.push('${product.externalId?.replaceAll("\'","\\\\\'")}'); | |
</g:iterateProductList> | |
injectCustomer(_data); | |
_data.siteType = 'd'; | |
_data.itemList = productList; | |
dataLayer = [_data]; | |
</g:if> | |
<g:if test="${params.page == 'product'}"> | |
injectProduct = function(obj){ | |
obj.productId = '${product.getExternalId()?.replaceAll("\'","\\\\\'")}'; | |
obj.productPrice = ${product.getSalePrice()}; | |
obj.productName = '${product.getName()?.replaceAll("\'","\\\\\'")}'; // valmont #naovaitercopa | |
obj.productCategory = '${product.getMain_category_name()?.replaceAll("\'","\\\\\'")}'; | |
obj.uri = '${product.uri ?: product.getDefaultUri()}'; | |
obj.imageUrl = '${product.mainImageUrl ?: ''}'; | |
obj.productSku = []; | |
<g:each in="${product.getSkuList()}"> | |
obj.productSku.push({ | |
id : '${it.externalId?.replaceAll("\'","\\\\\'")}', | |
sku : '${it.name?.replaceAll("\'","\\\\\'")}', | |
inventory : '${it.inventory}' | |
}); | |
</g:each> | |
}; | |
injectCustomer(_data); | |
injectProduct(_data); | |
_data.siteType = 'd'; | |
dataLayer = [_data]; | |
</g:if> | |
<g:if test="${params.page == 'cart'}"> | |
injectCart = function(obj){ | |
obj.cartId = "${session.productCart.id}"; | |
obj.cartTotal = ${session.productCart.saleAmount}; | |
obj.cartShipping = ${session.productCart.saleFreight}; | |
obj.cartDiscount = ${session.productCart.discount}; | |
obj.cartQuantity = ${session.productCart.items.quantity.sum() ?: 0}; | |
<g:if test="${session.productCart.cupom}"> | |
obj.cartPromoCode = "${session.productCart.cupom}"; | |
</g:if> | |
obj.cartProducts = []; | |
<g:each in="${session.productCart.items}"> | |
obj.cartProducts.push({ | |
productId : "${it.getIndexedProduct().externalId}", | |
productUrl : "${it.props.catalogUrl}", | |
imageUrl : "${it.getIndexedProduct()?.mainImageUrl ?: ''}", | |
productSku : '${it.productType.skuId}', | |
productName : '${it.productType.name?.replaceAll("\'","\\\\\'")}', | |
productPrice : ${it.getPriceWithDiscount()}, | |
productQuantity : ${it.quantity}, | |
productCategory : '${it.getIndexedProduct().getMain_category_name()?.replaceAll("\'","\\\\\'")}' | |
}); | |
</g:each> | |
}; | |
injectCustomer(_data); | |
injectCart(_data); | |
_data.siteType = 'd'; | |
dataLayer = [_data]; | |
</g:if> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment