Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GA 4 parameter does not work #228

Closed
ryan1-chang opened this issue Jul 9, 2021 · 6 comments
Closed

GA 4 parameter does not work #228

ryan1-chang opened this issue Jul 9, 2021 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@ryan1-chang
Copy link

GA 4 parameter does not work

[params]
gtagId = "G-XXXXXXXXXX"

I added the following code to this file

<!-- layouts/partials/head.html -->
<head>
  {{ if .Site.Params.gtagId }}
  {{ partial "google-analytics-gtag-async.html" . }}
  {{ end }}
  ...
</head>
@ryan1-chang ryan1-chang added the bug Something isn't working label Jul 9, 2021
@lxndrblz
Copy link
Owner

lxndrblz commented Jul 9, 2021

@ryan1-chang There is no need to include the partial in your head.html as it is included in the footer. Check out this line and make sure that both conditions are met:

{{- if and (hugo.IsProduction) (.Site.Params.gtagId) -}}

I've also checked your site and on there I could see the gtag code being loaded in the header, which you had added. What makes you believe that the parameter is not working?

@ghost
Copy link

ghost commented Jul 9, 2021

I can't seem to get this working either, even when manually setting HUGO_ENV = production
Using hugo v85 and the latest version of the theme.
Removing the IF AND statement and calling "partial "google-analytics-gtag-async.html" .context" on it's own seems to work.
I don't seem to be able to get any IF statements working in footer.html but copying the same code into header.html works fine.

@lxndrblz
Copy link
Owner

lxndrblz commented Jul 9, 2021

@ryan1-chang I was able to reproduce the error. It seems to have to do with the if condition. However, I haven't found out what triggers the behaviour.

@JackywithaWhiteDog
Copy link
Contributor

JackywithaWhiteDog commented Jul 10, 2021

Hi, I noticed that the problem is related to the if condition. Since the site variables are passed with the key context in a dictionary, calling .Site.Params.gtagId cannot access anything, and it should be changed to

{{- if and (hugo.IsProduction) (.context.Site.Params.gtagId) -}}

It seems to work for me after this modification, and I had sent a pull request #230.

Hope this helps.

@ghost
Copy link

ghost commented Jul 10, 2021

Good catch, just tested on mine and change works as expected

@lxndrblz
Copy link
Owner

@JackywithaWhiteDog Well spotted and thanks for your explanation.
I'll review the your PR soon. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants