Page specific Google Tag Manager conversion js snippet on modular page so tag fires in <head>

I just read through the Learn Grav Asset Manager chapter and cannot figure out for the life of me the syntax for loading a page specific js script (GTM/AdWords tag) <script>gtag adwords tracking stuff</script>. I found this suggestion on an archive forum post:

There are many ways to accomplish this, but for a single page, I would recommend two ways:

  1. Use the Assets plugin to add the css/js right in the markdown - GitHub - getgrav/grav-plugin-assets: Grav Assets Plugin 5
  2. Enable Twig processing in the page header (process: twig: true) and then use syntax like this:
    {% do assets.addJs('/mypage/special.js') %}

I would like to record goals/events in GA, GTM, and AdWords and be able to place these js tag snippets page specifically. The tag fires on whatever page it’s in, so it can only be on the specific page.

Thoughts?

Cheers,
.: pl

Thanks to Paul Massendari for helping me figure this out in the Slack group!

His suggestion below:

Philippe Levy [9:49 AM]
Hey Paul. I read all of your threads about putting page specific js snippets in the block on modular pages and I was hoping I could ask you some questions about it. I’m beating myself up trying to figure this out!

paul_massendari [11:43 AM]
hi Philippe, sure, ask away!

Philippe Levy [11:56 AM]
Awesome. The js snippet the asset manager is loading appears to be pipelined and placed below the footer when I look at page source instead of in the head. The code snippets I’m trying to place are just page specific Google Tag Manager snippets that are supposed to be placed in the and only fire on that page alone. The only way I have been able to figure out how to do this is to put all these snippets in base.html.twig with twig if statements with page.title conditions. There will be many of them, so doesn’t seem like a clean way to do it! What do you recommend?

paul_massendari [11:58 AM]
hmm I would not be worried about the multiple if statement. Keep in mind it takes only a few milisecond to process all of it
in this theme I rely on it a lot just for icons: https://github.com/paulmassen/grav-theme-chalk/blob/master/templates/partials/footer.html.twig
GitHub
paulmassen/grav-theme-chalk
grav-theme-chalk - Port of the Jekyll’s chalk theme to Grav CMS
are your snippet page specific or template specific?

Philippe Levy [12:00 PM]
I pasted the snippets in their entirety for each if but you’re saying the code snippets are only processed if the if condition is met?

paul_massendari [12:01 PM]
yes, sure
The snippet wont appear in your page source if you write it inside if statement
and the condition is not met

Philippe Levy [12:02 PM]
Ahh cool. Merci mon homme!

paul_massendari [12:03 PM]
ah au ! de rien!

Hi! I know this has been marked as solved, but why don’t you use GTM itself to fire the Tags based on page title using triggers? That’s what it was made for and you wouldn’t have to uglify your code.

1 Like

Hey Fabbow,

Thanks for the re:

In answer to your question, I didn’t realize the title changed on the “Thank You” page. Good idea.

Turns out I didn’t need a specific click snippet at all. That’s the old AdWords way of doing it apparently.

Turns out I can simply assign a made up class to the button I want associated to the conversion action and Google Tag Manager does the rest using the two global codes > global code and global code. The trigger is just the dummy class. Since I chose a “click” event action in the trigger GTM only fires the tag when the visitor clicks the submit action on the form.

THAT SAID. Your solution is better because when the visitor clicks on the submit it fires even if the form is invalidated. It still counts it as a conversion which isn’t good. With your method it counts the conversion when you get to the Thank You page with the new page title which has “Email sent” in it. I can just just “Email sent” as the trigger and it should be good. Just as a side note, there is no unique URL for the Thank You page which is why I didn’t use that method.

Thanks for the solution!

Hey there, glad to hear :slight_smile:

If your conversion is based on a form submission, you can also use the “Form Submission” Trigger in GTMs trigger types. This trigger checks for a valid form submit event and lets you fire Tags accordingly.

Indeed this makes all kinds of sense. I was trying to be clever about it and an obvious method was right there. I feel like performance tracking is incredibly important. I wonder how many members of the community are using these tools. Doesn’t seem to be much discussion about it. Cheers again Fabbow.