if (window.nzpa_block) {
  // Don't initialise the analytics code
}
else {
    // Tag Manager init
  window.dataLayer = [];
  try {
    // Init GTM dataLayer with variables from Drupal
    window.dataLayer.push(Drupal.settings.user.googleAnalytics.tagManager.dataLayer);
  } catch(e) {
    if (e instanceof TypeError) {
      if (typeof console == 'object') console.log('Tag Manager params are not available or are invalid in Drupal.settings', e);
    }
  }

  // Upstream GTM code
  (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',"GTM-5XL95BV");
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  (function() { // wrap up our code to keep global scope clean
    try {
      var currentUser = Drupal.settings.user;
      if (currentUser.googleAnalytics) {
        ga('create', currentUser.googleAnalytics.trackerData);

        if(typeof Drupal.settings.second_tracker != 'undefined') {
          // OT313 -- Create the second tracker here
          ga('create', currentUser.googleAnalytics.trackerDataYS.trackingId, 'auto', {'name': 'youshop_tracker'});
        }

        // if user is logged-in / has a GUID
        if (typeof currentUser.googleAnalytics.userData != 'undefined') {
          if (currentUser.googleAnalytics.userData.guid) {
            ga('set', '&uid', currentUser.googleAnalytics.userData.guid); // in BETA @see https://developers.google.com/analytics/devguides/collection/analyticsjs/user-id - we suspect this might change, hence the namespaced fieldname
          }
        }

        // if we have enabled the in-page enhanced link attribution
        if (typeof currentUser.googleAnalytics.linkAttribution != 'undefined') {
          if (currentUser.googleAnalytics.linkAttribution.enabled) {
            ga('require', 'linkid', 'linkid.js');
          }
        }

        // if we have enabled enhanced commerce
        if (typeof currentUser.googleAnalytics.enhancedCommerce != 'undefined') {
          if (currentUser.googleAnalytics.enhancedCommerce.enabled) {
            ga('require', 'ec');
            if (typeof Drupal.settings.NZPostEnahncedCommerceImpressions != 'undefined') {
              for(var impression_idx in Drupal.settings.NZPostEnahncedCommerceImpressions) {
                var impression = Drupal.settings.NZPostEnahncedCommerceImpressions[impression_idx];
                ga('ec:addImpression', impression);
              }
            }
          }
        }
      }

      // custom variables destined for UA must have a 'uaDimension' otherwise they'll be ignored
      // custom variables without a 'uaDimension' are most likely here for Tag Manager
      for(var varName in currentUser.googleAnalytics.customVars) {
        var customVar = currentUser.googleAnalytics.customVars[varName];
        if (customVar.hasOwnProperty('uaDimension') && customVar.hasOwnProperty('value')) {
          if (customVar.uaDimension) { // UA dimensions must be numeric and greater than ZERO
            ga('set', 'dimension'+customVar.uaDimension, customVar.value);
          }
        }
      }
    } catch(e) {
      if (e instanceof TypeError) {
        if (typeof console == 'object') console.log('UA params are not available or are invalid in Drupal.settings', e);
      }
    }
  })();
  }