Coder Social home page Coder Social logo

shopify-datalayer-code's Introduction

Shopify-DataLayer-code

<script>
  var gtmId = "GTM-P2LDDTT";

  (function() {
    window.dataLayer = window.dataLayer || [];
    dataLayer.push({ ecommerce: null });

    // Prevent double-tracking if an upsell is seen or the order status page has been seen
    {% if first_time_accessed == true and post_purchase_page_accessed == false %}
    var checkout = window.Shopify.checkout;
    dataLayer.push({
      event: "dl_purchase",
      ecommerce: {
        transaction_id: "{{ order.order_number }}",
        affiliation: "{{ shop.url }}",
        coupon: getDiscountAmount(),
        currency: checkout.total_price_set.presentment_money.currency_code,
        items: items(),
        shipping: checkout.shipping_rate.price,
        tax: checkout.total_tax,
        value: checkout.total_price_set.presentment_money.amount
      }
    });

    function getDiscountAmount(line_item_count) {
      if (checkout.discount === null || typeof checkout.discount === 'undefined') return '0';
      if (checkout.discount.length === 0) return '0';
      if (line_item_count === undefined) {
        return checkout.discount.amount;
      } else {
        if (checkout.line_items[line_item_count].discount_allocations[0].amount === null || typeof checkout.line_items[line_item_count].discount_allocations[0].amount === 'undefined') return '0';
        if (checkout.line_items[line_item_count].discount_allocations[0].amount === 0) return '0';
        return checkout.line_items[line_item_count].discount_allocations[0].amount;
      };
    };

    function items(){
      var jsonarray = [];
      for (var line_item_count = 0; line_item_count < checkout.line_items.length; line_item_count++){
        var jsonobj = {
          item_id: checkout.line_items[line_item_count].product_id,
          item_name: checkout.line_items[line_item_count].title,
          currency: checkout.total_price_set.presentment_money.currency_code,
          discount: getDiscountAmount(line_item_count),
          price: checkout.line_items[line_item_count].line_price,
          quantity: checkout.line_items[line_item_count].quantity
        };
        jsonarray.push(jsonobj)
      };
      return jsonarray;
    };
    {% endif %}

    // Google Tag Manager
    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer',gtmId);
  })();
</script>


other

{% if first_time_accessed %}<script>                               
	window.dataLayer = window.dataLayer || [];                                            
	var shipping_price = '{{shipping_price | money_without_currency }}';
	shipping_price  = shipping_price.replace(",", ".");
	var total_price = '{{total_price | money_without_currency }}';
	total_price  = total_price.replace(",", ".");
	var tax_price = '{{tax_price | money_without_currency }}';
	tax_price  = tax_price.replace(",", ".");
	window.dataLayer.push({
	'page_type': 'purchase',
	'event': 'websensepro_purchase',
	'currency': "{{ shop.currency }}",
	'totalValue': total_price,
	'shipping': shipping_price,
	'tax': tax_price,
	'payment_type': '{{order.transactions[0].gateway}}',
	'transaction_id': "{{order.name}}",
	});</script> 
	{% endif %}

shopify-datalayer-code's People

Contributors

r-jee avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.