Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.8 kB
3
Indexable
<script>
  window.addEventListener('load',function(){
  var id  ='';
  var price ='';
  var pagetype ='';
  var path_url= window.location.pathname;
  var href_url =window.location.href;
  var event_name ='';
  //home-page
  if(path_url=="/"){
    pagetype="home";	
    event_name ='home';
  }
  //category page
  else if(jQuery('.woocommerce-ordering').length == 1){
    event_name ='view_item_list';
    pagetype='category';
  }

  //product page
  else if(jQuery('.single-product').length == 1){
    event_name ='view_item';
    id = pysOptions .woo.singleProductId;
    pagetype='product';
    price = parseFloat(jQuery('.product [class="woocommerce-Price-amount amount"]:first').text().replace(/[^0-9.]/g,''));
  }
  //cart page
  else if(jQuery('.woocommerce-cart').length == 1){
    event_name ='add_to_cart';
    var len =jQuery('.cart_item [data-product_id]').length;
    var id = new Array();
    for (i = 0; i < len; i++) {
      var temp_id =jQuery('.cart_item [data-product_id]')[i].getAttribute('data-product_id');
      id.push(temp_id); 
    }
    pagetype='cart';
    price = jQuery('.order-total [class="woocommerce-Price-amount amount"]:first').text().replace(/[^0-9.]/g,'');
  }
  //order confirmation page
  else if(window.location.href.indexOf("order-received")!=-1){
    event_name ='purchase';
    pagetype='purchase';
  }
  //other pages
  else {
    event_name ='other';
    pagetype='other';
  }

  var google_tag_params = {
    ecomm_prodid: id, 
    ecomm_pagetype: pagetype,
    ecomm_totalvalue: parseFloat(price)
  };


  gtag('event',event_name, {
    'ecomm_prodid':google_tag_params.ecomm_prodid,
    'ecomm_pagetype':google_tag_params.ecomm_pagetype,
    'ecomm_totalvalue':google_tag_params.ecomm_totalvalue, 
    'google_business_vertical': 'retail'
  });
})
</script>