Skip to content

How to fire a GTM tag on the first page view of a session

Google Tag Manager is so flexible and easy to use, once you are familiar with it, that it’s surprising when you can’t easily do something.

I wanted to fire a tag only on the first page view to display a dialogue window to visitors.

To prevent this being too intrusive, I didn’t want the dialogue window to be shown on subsequent page views. It sounds like the sort of thing that would be fairly common to do in GTM with a trigger condition and there must be other use cases for this.

The page the visitor lands on was irrelevant so I couldn’t fire the trigger based on page path or URL. But there aren’t any built-in GTM triggers for first page views. I knew there must be a solution but was really struggling to work it out until I came across this article: Fire a tag only on the first page view with Google Tag Manager from Julius Fedorovicius and it’s the best solution I have found.

It works like this:

  • On each page view, use a GTM page view trigger to fire a custom HTML GTM tag containing some Javascript.
  • When first called, the Javascript sets a cookie with a GTM variable that records the number of page views.
  • Each subsequent page views increments the variable.
  • Now you can update your trigger that fires whatever it is you want to do once so it is only triggered when the cookie is either 1 or undefined (just in case the cookie hasn’t been created at the time this tag is first called).

That’s it! A smart solution and not one that I can claim credit for but I wanted to share how Julius has done it, in case it helps you out like it helped me out.

You can find the sample code at https://www.analyticsmania.com/post/fire-a-tag-only-on-the-first-page-view-with-google-tag-manager/

Do you have a better solution? Send me a solution at @swaffs

Published indigital tips

Comments are closed.