This code is how to measure a transaction with enhance ecomm instead of ecomm tracking
-
-
Save philcon93/64aa032458682053c0a7918c7b2349e3 to your computer and use it in GitHub Desktop.
enhancedgoogleanalytics
This file contains hidden or 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
ga('create', 'UA-XXXXX-Y'); | |
ga('require', 'ec'); | |
### Measuring a Transaction | |
<!-- Start Google Enhanced E-commerce --> | |
[%SHOW_ORDER id:'[@ORDER_ID@]'%][%PARAM *header%] | |
<script type="text/javascript"> | |
ga('set', 'currencyCode', 'AUD'); | |
// Transaction level information is provided via an actionFieldObject. | |
ga('ec:setAction', 'purchase', { | |
'id': '[%URL_ENCODE%][@ORDER_ID@][%/ URL_ENCODE%]', | |
'affiliation': '[%URL_ENCODE%][@CONFIG:WEBSITE_NAME@][%/ URL_ENCODE%]', | |
'revenue': '[@GRAND_TOTAL@]', | |
'tax': '[@tax_total@]', | |
'shipping': '[@shipping_price@]', | |
}); | |
[%END PARAM%] | |
[%PARAM *body%] | |
ga('ec:addProduct', { | |
'id': '[%URL_ENCODE%][@SKU@][%/ URL_ENCODE%]', | |
'name': '[%URL_ENCODE%][@MODEL@][%/ URL_ENCODE%]', | |
'category': '[%URL_ENCODE%][@CATEGORY_NAME@][%/ URL_ENCODE%]', | |
'brand': '[%URL_ENCODE%][@brand@][%/ URL_ENCODE%]', | |
'price': '[@PRICE@]', | |
'quantity': [@QTY@] | |
}); | |
[%END PARAM%] | |
[%PARAM *footer%] | |
ga('send', 'pageview'); // Send transaction data with initial pageview. | |
</script> | |
[%END PARAM%][%END SHOW_ORDER%] | |
<!-- End Google Enhanced E-commerce --> |
This file contains hidden or 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
// <!-- Measuring an Addition or Removal from Cart --> | |
ga('require', 'ec'); | |
function metricVal(type, qty, price){ | |
if(type == 'remove'){ | |
console.log(-(price*qty)); | |
return -(price*qty); | |
}else{ | |
console.log(price*qty); | |
return price*qty; | |
} | |
} | |
var product = { sku: '', name: '', price: 0, qty: 0 } | |
function resetProduct(){ | |
product = { sku: '', name: '', price: 0, qty: 0 } | |
} | |
$('body').on('click', '.addtocart', function(){ | |
resetProduct(); | |
var productRel = $(this).attr('rel'); | |
product['sku'] = $('input[id=sku'+productRel+']').val(); | |
product['name'] = $('input[id=model'+productRel+']').val(); | |
product['price'] = 6.9; | |
product['qty'] = $('input[id=qty'+productRel+']').val(); | |
console.log(product); | |
gaCart(product, 'add'); | |
}); | |
$('#neto-dropdown').on('click', '.right > a', function(){ | |
resetProduct(); | |
var productHref = $(this).attr('href'); | |
var cartItems = $(this).parents('.right').find('.title a').text(); | |
var skuRegExp = /\'([^']+)\'/; | |
var sku = skuRegExp.exec(productHref)[1]; | |
product['sku'] = sku; | |
product['name'] = cartItems.split('x ')[1]; | |
product['price'] = 6.9; | |
product['qty'] = cartItems.split('x ')[0]; | |
console.log(product); | |
gaCart(product, 'remove'); | |
}); | |
$('.cartTable').on('click', '.btn-danger', function(){ | |
resetProduct(); | |
var productImg = $(this).parents('tr').find('.cartTable--column-image img').attr('src'); | |
var skuRegExp = /[\w-]+\.(jpg|png|txt)/g; | |
var sku = skuRegExp.exec(productImg)[0]; | |
sku = sku.split('.')[0]; | |
var productDetails = $(this).parents('tr').find('h4').text().split('$'); | |
var qty = $(this).parents('tr').find('.cart-qty').attr('value'); | |
product['sku'] = sku; | |
product['name'] = productDetails[0]; | |
product['price'] = productDetails[1]; | |
product['qty'] = qty; | |
console.log(product); | |
gaCart(product, 'remove'); | |
}); | |
function gaCart(product, type){ | |
ga('ec:addProduct', { | |
'id': product.sku, | |
'name': product.name, | |
'price': product.price, | |
'quantity': product.qty | |
// 'metric1': metricVal(type, product.qty, product.price) | |
}); | |
ga('ec:setAction', type); | |
ga('send', 'event', 'UX', 'click', 'add to cart'); // Send data using an event. | |
} |
This file contains hidden or 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
<!-- Measuring a Promotion Clicks --> | |
[%if [@config:current_page_type@] eq 'home'%] | |
[%site_value id:'footer_javascript'%] | |
<script type="text/javascript"> | |
ga('create', 'UA-XXXXX-Y'); | |
ga('require', 'ec'); | |
$('body#n_home').on('click', '#homepageCarousel a', function(e){ | |
e.preventDefault(); | |
console.log('google time!'); | |
var promoLink = $(this).attr('href'); | |
var promoId = $(this).parent('.item').attr('id'); | |
var promoName = $(this).find('h3').innerHTML; | |
ga('ec:addPromo', { | |
'id': promoId, | |
'name': promoName | |
}); | |
// Send the promo_click action with an event. | |
ga('ec:setAction', 'promo_click'); | |
ga('send', 'event', 'Internal Promotions', 'click', promoName, { | |
hitCallback: function() { | |
document.location = promoLink; | |
} | |
}); | |
}); | |
</script> | |
[%/site_value%] | |
[%/if%] |
This file contains hidden or 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
[%if [@config:current_page_type@] eq 'product'%] | |
<!-- Google Analytics Product Impression--> | |
<script> | |
ga('create', 'UA-XXXXX-Y'); | |
ga('require', 'ec'); | |
ga('ec:addProduct', { | |
'id': '[%URL_ENCODE%][@SKU@][%/ URL_ENCODE%]', | |
'name': '[%URL_ENCODE%][@MODEL@][%/ URL_ENCODE%]', | |
[%content_path id:'[@inventory_id@]' type:'category' limit:'1'%] | |
[%param *body%] | |
'category':'[%URL_ENCODE%][@content_name@][%/URL_ENCODE%]', | |
[%/param%] | |
[%/content_path%] | |
[%list type:'content' filter:'content_type=brands&sku=[@sku@]' limit:'1'%] | |
[%param *body%] | |
'brand': '[%URL_ENCODE%][@content_name@][%/ URL_ENCODE%]' | |
[%/param%] | |
[%/list%] | |
}); | |
ga('ec:setAction', 'detail'); | |
</script> | |
<!-- End Google Analytics Product Impression--> | |
[%/if%] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment