React + Router + Google Tag Manager

前端 未结 2 740
北荒
北荒 2021-01-31 10:07

I\'ve been spending a bit of time developing an MVP at quickcypher.com. I wanted to start putting in some analytics, and it worked great for just tracking total visits, but thin

2条回答
  •  失恋的感觉
    2021-01-31 10:55

    A bit late to the party here, but react router should need no special code to integrate with GTM. Just drop the GTM script on your page (immediately after the opening tag as recommended) and let your app run as normal.

    In GTM create a custom trigger for history change.

    You can fire it on all history changes.

    Or only on some of them. Only on your production hostname, for example.

    Then add a tag for your google analytics (or whatever) and configure it to fire on your history change event by clicking "More" under "Fire On" and selecting the trigger created above.

    It's also important to change the Advanced Settings of our tag to fire once per event instead of once per page. Without this, the tag will only fire on the initial page load.

提交回复
热议问题