Support Forums

Snippet Injection before </head> not working


I tried to use Netlify’s Snippet Injection feature to inject my Global site tag (gtag.js) before the </head>. However, it doesn’t seem to be working.

Below is the exact snippet I am trying to inject. Everything else seems to be in order, so I don’t know why this isn’t working. I’ve looked at previous articles, and done all the steps. Enabling and doing a deploy doesn’t fix it.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=INSERT ID HERE"></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'INSERT ID HERE');

Any help would be appreciated, thanks!

Hey @p0w3r_zurg3

I was able to deploy and inject the snippet before the </head> without issue.

How are you deploying? What are you deploying (simple HTML, SPA, etc)?

Are you able to provide you site URL?

Hi @colemay

I’m deploying a React + Next.js Webapp. I’m deploying from GitHub. The URL is https://stempump.org .


I am unable to reproduce this. I have tested inject this snippet into React, Next.js, and NuxtJS and all were successful, all deployed from GitHub. You are adding the snippet in the UI (docs) or an alternate method?

I’m adding them via the UI in the Post Processing section. I have no clue to why it isn’t working, as everything seems to be correct. There’s a clear .

Out of curiosity, have you tried a Clear cache and deploy site? Not 100% sure this will make a different, but worth a shot.
Screen Shot 2021-07-26 at 8.58.58 am

And for the sake of completeness, this is what I have

Just tried that, no change. I’m really confused to why this is happening at all.

Hey @p0w3r_zurg3

It looks like you have a redirect rule that redirects "/" to a function and we don’t inject snippet injections with the function response. If you remove the redirect rule the drawer will work but it seems like you’re using SSR and that may not be an option.

 [ 0] {
            "path" => "/",
              "to" => "/.netlify/functions/next_index",
          "status" => 200,
         "pattern" => "^/$",
        "captures" => []

The below is quoted from @fool’s response here:

…for functions (and other proxy’d content), you’ll have to make sure ALL required output is generated directly by the function/remote server, rather than relying on us to modify the outgoing data stream.

Hope this helps!

1 Like