GTM randomly skips initial pageview in single page app

偶尔善良 提交于 2019-12-01 02:21:10

As @kemsky suggested, GTM lifecycle is tied to internal gtm.load event, which happens on window onload. So DOMContentLoaded may be too early to bootstrap.

Considering that GTM script was loaded prior to SPA script,

window.addEventListener('load', () => {
  platformBrowserDynamic().bootstrapModule(MyAppModule);
});

callback will be triggered when GTM is ready to receive history change events, and there should be no race conditions.

It looks like analytics scripts are loaded with <script async=true>:

j.async = true; or a.async = 1;

Try to remove async and see if it helps.

You may change the trigger of your UA pageview tag from plain 'pageview' and stick to custom event fired at NavigationEnd

router.events.subscribe(e => {
  if (e instanceof NavigationEnd) {
    dataLayer = window.dataL
    dataLayer.push({'event':'custom pageview event'});
  }
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!