Cheat and Win with Google Tag Manager: Easy Dynamic Content Tracking


Sometimes Tag Manager is so easy it feels like cheating. In a good way. Like getting a super acorn power-up and turning into Flying Squirrel Mario.


Recently I used my flying squirrel powers to beat the dynamic content boss. My client wanted to track how visitors used a couple different search forms, each with multiple options. Every time an option was selected, new search results would appear dynamically and a parameter would be added to the URL hash.

For example, if the visitor chose to view all the seminars on day 1, the URL would become /seminarsearch#day=1. Search those seminars by topic X and the URL would change to /seminarsearch#day=1&topic=x.

Or it might be a different search form, say for vendors, and the URL might look like /vendorsearch#category=abc&location=bldg2.

How was I going to tackle all those moving parts and get the data I needed into Google Analytics? I wrote a couple different pieces of code that were unsatisfactory for one reason or another.

And then I found my super acorn.


Deceptively simple! Don’t worry about listening for clicks and trying to identify if they’re the clicks I’m interested in. Just detect when the hash changes and send an event to the data layer. This tag fires on every search form page.

Now I use the data layer event to control all kinds of tracking. When a visitor interacts with one of my search forms, an event rule (“event=hashchange”) fires these tags:

  1. a virtual pageview – it’s a new page of content, so I treat it that way – I capture the URL with the hash, which means for each of those extended URLs I get all the page metrics in my GA content reports (not to mention more useful flow viz reports and the ability to create goal funnels, although I haven’t done that yet)
  2. a search form click event – so I can get site usage and ecommerce metrics, along with the hierarchy of the GA event reports
  3. plus scroll tracking events! – from Justin Cutroni’s scroll tracking code, which I adapted for Tag Manager (did the visitor start scrolling through the search results and did they scroll all the way to the bottom? I included the custom variable for how fast they scrolled)

Here’s the adapted part (get the entire original tag here):

The scroll tracking tag fires either after the window loads or when the hash changes, with a simple revision of the rule from “event equals hashchange” to “event matches RegEx gtm.load|hashchange”. That way I don’t have to wait for the visitor to click any options, in case they start scrolling through the default content (featured seminars or vendors).

And then finally I have tags that fire when the scroll tracking tag pushes events to the data layer. These tags send my events and custom variables to Google Analytics.

custom var gtm

Remember: to make sure your custom variable info is not just set, but also sent, hitch it to an event. Tag Manager makes it easy with the options under “More Settings” as shown above.

Has dynamic content made it more difficult to get the data you need to answer important questions? How have you tackled it? Please share in the comments.

Dorcas Alexander is a Manager for the Analytics & Insight department. Her path to LunaMetrics followed stints in ad agency creative, math, and computer science. Dorcas has a master's degree in language and information technologies from Carnegie Mellon University, where she helped build precursors to a Universal Translator. One of the top-rated tournament Scrabble players in Pennsylvania, Dorcas has an insatiable drive to compete and win.

  • Hi Dorcas,

    Thank you so much for this article. I have found very little information regarding the correct use of Google Tag Manager, for more complicated subjects like Dynamic Custom Variables and javascript.

    I would love to read any more articles that you have!


  • Lorraine

    Hi Dorcas,

    Thanks for the article. Great reading with easy to follow instructions.

    Did you publish the window.onhashchange script through Tag Manager or place it directly in the source code of the page. I have been trying to do something very similar for a while now and the problem I have is that because the anchored page is not a new page load, GTM doesn’t load up tags unless I refresh the page.


    • Dorcas Alexander

      Hi Lorraine, Yes, I have window.onhashchange in a tag, not in the source code of the page. Instead of using a rule to fire your tag on the anchored page (which you’ve found won’t work anyway), use a rule that fires it on the non-anchored page path. You should have other tags that can fire later based on event=hashchange. But the tag that detects the hash change can fire earlier and sit there waiting to do its job. Hope that helps!

  • Primož


    I just started using google tag manager. I managed to merge different domains to a single analytics property and some other things. Now I am trying to track hashtags as pageviews so I can create Goal funnel with them.

    My question is: where do I put your “super acorn”, under custom html or..? And will this help me with hashtag tracking as pageview?

    • Dorcas Alexander

      Hi Primož, Create a custom HTML tag and insert the super acorn script. Then create a pageview tag that fires when event = hashchange, and in this tag set up the virtual pageview under More settings > Basic Configuration > Virtual Page Path. You can send whatever you want as the virtual page path. In my tag, I send window.location.pathname + + window.location.hash.

  • rsgracey

    Hi, Dorcas– perhaps I’m just too much of a noob, but there isn’t nearly enough detail here. For instance, how do you put a script in a tag?–or is it somehow a macro, and if so, is it the category, action, etc?

    I’m trying to count it as an event, rather than a virtual page view because all my other “pages” are counted that way.

    Could you please put a few sequential steps together?

    I am greatly obliged to you for your help–

    • Dorcas Alexander

      Hi Stephen, Short answer: About two months after I wrote this post, Google Tag Manager added a feature that automatically detects a change in the browser history even if the page did not reload. You may want to use that method instead of this one, which requires a couple extra steps. Long answer: In Google Tag Manager, you can create 3 types of items: tags, rules, and macros. My super acorn script appears (just as you see it in the first code box above) in a tag, where I have chosen the tag type: “Custom HTML”. I pasted the entire script into the HTML box of this tag. Then I set a rule to fire the tag, where the rule has one condition: the URL of the page must match the URL format of my search form pages. All of this is only a setup to let me know that the hash fragment of the URL has changed. Tracking events or virtual page views or anything else must come in a separate tag or tags. Create a GA event tag and fire it with a rule in which one of the conditions is “event equals hashchange” and the other condition is what type of URL you want to match (or you can fire it on all pages). What you choose to send as your event category and action is up to you, but I suggest starting with a fixed event category name, e.g. “hashchange” and a dynamic event action based on the contents of the hash fragment of the URL. I created a macro of type “URL” with “component name = fragment” to capture the hash fragment. Then I selected that macro from the dropdown list for event action (in my event tag).

  • Vik

    Hi Dorcas, not sure if you’re still getting comments on this post but seems there’s little material on this subject on the web. According to Google, one of the benefits of the Tag Manager is that it makes marketers’ lives relatively less dependent on developers. Having said that, as a marketer I’m still having a hard time figuring out if the Tag Manager is a replacement to or a complement of GA.
    In other words, to track my content assets, do I still have to go through the usual process of UTM parameter generation on GA and then use the Tag Manager for monitoring purposes, or, if I can now use the Tag Manager as my one and only source to creating and assigning tags (including UTM parameters) to content assets without having to use GA for that purpose?


    • Dorcas Alexander

      Hi Vik, Tag Manager does not replace GA as the way to track your site users. Tag Manager delivers your GA code (and many other types of tracking code) to the page. Instead of putting multiple code snippets on the page — one for GA, one (or more) for AdWords, etc. — you put one snippet of Tag Manager code on the page. Then you go into the Tag Manager interface and create tags for each type of tracking you want to do — such as a GA pageview tag, GA event tags, etc. Tag Manager does not create reports where you see your UTM parameters — you still must go to GA to see those, and all other reports. You should not be placing UTM parameters on your own site and thus should not be delivering them via Tag Manager. Instead, you should only place UTM parameters on links to your site when you deliver those links via email or non-AdWords paid search or other places not on your site. Track internal banners and promotions on your own site with events.

      • Vik

        Hi Dorcas, thank you for your timely response. Yes, you’re certainly right about using UTM parameters exclusively on external links. Then again, although I have separate UA & Adwords codes on my website, I’m still not so sure if it’s worthwhile migrating to Tag Manager. Your final piece of advice?

        • Dorcas Alexander

          If you use on-page code only for GA pageviews and AdWords remarketing/conversions, then you do not need to migrate to Tag Manager. However, if you want to track events and/or ecommerce (either of which will greatly enhance your GA data), then you will find migrating to Tag Manager very useful.

  • Hi Dorcas! When I look at the code samples it looks like some HTML/CSS has been injected into the raw code. Are you seeing the same thing?

Contact Us.

Follow Us



We'll get back to you
in ONE business day.
Our Locations
THE FOUNDRY [map] LunaMetrics

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203


4115 N. Ravenswood
Suite 101
Chicago, IL 60613


2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187