Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module): allow client side push before init #60

Merged
merged 1 commit into from
Aug 3, 2020

Conversation

1ed
Copy link
Contributor

@1ed 1ed commented Jul 27, 2020

With this patch we can push client side events before the container loaded event:

export default function gtmPlugin({ app, $gtm, $storage }) {
  if (app.$config.GTM_ID) {
    $gtm.push({ data: $storage.get('key') }); // this data only available in localstorage 
    $gtm.init(app.$config.GTM_ID);
  }
}

Kind of a follow up on #51

This is the diff for the compiled script:

if (!window._gtm_init) {
  window._gtm_init = 1;
  (function (w, n, d, m, e, p) {
    w[d] = (w[d] == 1 || n[d] == 'yes' || n[d] == 1 || n[m] == 1 || (w[e] && w[e].p && e[e][p]())) ? 1 : 0
  })(window, 'navigator', 'doNotTrack', 'msDoNotTrack', 'external', 'msTrackingProtectionEnabled');
  (function (w, d, s, l, x, y) {
-   w[l] = w[l] || [];
-   w[l].push({'gtm.start': new Date().getTime(), event: 'gtm.js'});
    w[x] = {};
    w._gtm_inject = function (i) {
      if (w.doNotTrack || w[x][i]) return;
      w[x][i] = 1;
+     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);
      j.async = true;
      j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i;
      f.parentNode.insertBefore(j, f);
    }
  })(window, document, 'script', 'dataLayer', '_gtm_ids', '_gtm_inject')
}

@pi0 pi0 changed the title fix(module): client side push before init fix(module): allow client side push before init Aug 3, 2020
@pi0 pi0 merged commit c9b073a into nuxt-community:master Aug 3, 2020
@1ed 1ed deleted the dataLayer-before-container-loaded branch August 15, 2020 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants