Skip to content

Instantly share code, notes, and snippets.

@philcon93
Last active June 5, 2018 02:20
Show Gist options
  • Select an option

  • Save philcon93/2aab37655db3f266db3772d92b33ac61 to your computer and use it in GitHub Desktop.

Select an option

Save philcon93/2aab37655db3f266db3772d92b33ac61 to your computer and use it in GitHub Desktop.
Google analytics script update

Migrate from analytics.js to gtag.js

analytics.js uses trackers to send data to Google Analytics and hit types to specify the types of the data.

Unlike analytics.js, gtag.js doesn't use trackers to send data to Google Analytics. It sends data to Google Analytics properties identified by their tracking IDs set by the config command. The event names supplied to gtag.js specify the types of data being sent to Google Analytics.

Page Header

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=[@REFERRAL_KEY1@]"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', '[@REFERRAL_KEY1@]'
  [%if [@CONFIG:CHECKOUT_COMPLETED@] %], {'page_title' : 'Invoice', 'page_path': '/purchase/invoice.html' }
  [%elseif [@CONFIG:GA_FUNNEL_VALUE@]%]%], {'page_path': '[@CONFIG:GA_FUNNEL_VALUE@]' }
  [%/if%]);
</script>

Thank you page

<!-- Start Google Purchase Tracking -->
[%set [@ga_cart_items@]%]0[%/set%]
[%show_order id:'[@order_id@]'%]
[%param *header%]
<script type="text/javascript">
    gtag('event', 'purchase', {
      'transaction_id': '[%escape%][@order_id@][%/escape%]', // Transaction ID. Required.
      'affiliation': '[%escape%][@CONFIG:WEBSITE_NAME@][%/escape%]',
      'value': '[@grand_total@]',
      'currency': '[@CONFIG:DEFAULTCURRENCY@]',
      'tax': '[@tax_total@]',
      'shipping': '[@shipping_price@]',
      'items': [
[%/ param%]
[%param *body%]
    [%set [@^ga_cart_items@]%][%calc [@ga_cart_items@] + [@qty@]/%][%/set%]
    {
      'id': '[%escape%][@sku@][%/escape%]', // SKU. Required.
      'name': '[%escape%][@model@][%/escape%]',
      'category': '[%escape%][@CATEGORY_NAME@][%/escape%]',
      'price': '[@price@]',
      'quantity': '[@qty@]'
    }[%if [@^ga_cart_items@] < [@^total_items@]%],[%/if%]
[%/ param%]
[%param *footer%]
        ]
    });
</script>
[%/ param%]
[%/ show_order%]
<!-- End Google Purchase Tracking -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment