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

bug Google Analytics has stopped working #955

Open
javierasping opened this issue Jul 3, 2024 · 8 comments
Open

bug Google Analytics has stopped working #955

javierasping opened this issue Jul 3, 2024 · 8 comments

Comments

@javierasping
Copy link

Since June 3rd, when the Git bot applied the following commit --> javierasping/sentinel@0cf1c66, my Google Analytics stats have stopped working. I have been looking into it and the script is not being inserted into the HTML code. My Google tag is G-GVDYVWJLRH.

When I search for it in the generated HTML code, I cannot find it.

javiercruces@HPOMEN15:~/Documentos/sentinel/public$ grep -r "G-GVDYVWJLRH" .

My website is in www.javiercd.es .

I have tried to do as in previous cases where you helped me update to the latest version of Hugo as well as update the dependencies using the following commands:

# update to latest version of all modules
hugo mod get -u

# update the npm dependencies
hugo mod npm pack

# cleanup go.sum file
hugo mod tidy

# install npm dependencies
npm install

My current version of Hugo is:

javiercruces@HPOMEN15:~/Documentos/sentinel$ hugo version
hugo v0.128.1-0ff542b4b9cc7b5cb425bbab8bdb5aace81d0c03+extended linux/amd64 BuildDate=2024-07-02T06:46:41Z VendorInfo=gohugoio

Thank you very much in advance for your help, best regards!!

@mgnkir
Copy link

mgnkir commented Aug 1, 2024

Same problem here

@deining
Copy link
Contributor

deining commented Aug 31, 2024

Since June 3rd, when the Git bot applied the following commit --> javierasping/sentinel@0cf1c66, my Google Analytics stats have stopped working.

In your config.yaml, I see this code which is incorrect IMHO:

142 # Enable Analytics
143 analytics:
144   enabled: true
145   services:
146     # Google Analytics
147     google:
148       id: G-GVDYVWJLRH

According to the Hugo documentation, the Google analytics ID should be go here:

services:
  googleAnalytics:
    id: "G-GVDYVWJLRH"

In your config.yaml, I can't see a services section at the root level at all. Can you please add one and report back if that cures your problem?

Remark: In case this fixes your problem, this quirk should be fixed in the repo hugo-toha.github.io as well.

@javierasping
Copy link
Author

Good morning and thank you very much for your interest. I have tried what you suggested, but the problem persists. Google Analytics is not being inserted into the page code. As a temporary solution, I have manually added the following code:

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

  gtag('config', 'G-GVDYVWJLRH');
</script>

In previous versions, this or something similar was automatically added by the theme, but since the commit I mentioned, it stopped working that way, so I have temporarily resolved it by adding it manually.

I have added the quotation marks as you mentioned earlier and applied the changes to match the official Hugo documentation, but it hasn't had any effect.

Thank you very much, but none of this has solved the issue!!

@deining
Copy link
Contributor

deining commented Sep 1, 2024

Good morning and thank you very much for your interest.

You are welcome.

I have tried what you suggested, but the problem persists.

Sorry to hear that. I checked out your site (javierasping/sentinel@0cf1c66) and was able to resolve your issue. I'm quite confident my advice given above is valid and will resolve your issue once applied correctly.

Can you please download this file to the root folder of your site:

hugo.yaml.txt

Once downloaded, please delete existing file config.yaml, afterwards rename the downloaded file:

hugo.yaml.txt   --> hugo.yaml   # GitHub does not allow upload of .yaml files!

Afterwards run hugo (using latest version 0.133.1) and you will find the google analytics script in the html source of your files again.

Does this work for you?

@javierasping
Copy link
Author

Thank you very much for responding and showing interest!!!! I tried your change with my current repository version and updated Hugo to the latest version.

javiercruces@HPOMEN15:~/Documentos/sentinel$ ls -l
total 240

**-rw-r--r--   1 javiercruces javiercruces   8138 Aug 31 20:00 _config_.yaml.txt**
-rw-r--r--   1 javiercruces javiercruces   8055 Sep  1 22:30 hugo.yaml

javiercruces@HPOMEN15:~/Documentos/sentinel$ hugo version
hugo v0.133.1-47d00202e7e61769ce4d14691e43b27852c9cce4+extended linux/amd64 BuildDate=2024-08-26T13:58:46Z VendorInfo=gohugoio

I’m not sure if you have checked in a post that does not have the analytics script; manually verify in the HTML code that the script has not been inserted automatically. I am currently writing new posts and have tried your solution, but it did not work.

I will provide you with an example of a page with the script manually added and another where the script has been removed. Hugo does not add the analytics automatically.

image

image

I’m not sure if it’s because with the update the analytics work differently, but in older versions, if I searched for my tag in the HTML, I could verify that Hugo was adding it. In a class activity, I had to submit the HTML code of the generated page. You can check that in any HTML file it is included without me manually adding it in the Markdown file.

I’m providing you with the link in case you want to check it: https://github.com/javierasping/CodigoHTML_sentinel/blob/main/categories/index.html.

Thank you in advance for your help. If, as you mentioned, the analytics are being generated locally, please verify that the analytics script is not manually added in the post file. I always place it first, right below the title, date, and other details (I’ve been doing this since I noticed that the graph in Google Analytics was at 0).

If there’s anything else I can try, please let me know. Thank you very much for your help!

I’m trying to respond as quickly as possible since you’re helping me. Today, you caught me in front of the computer, so I was able to reply quickly. ;)

@deining
Copy link
Contributor

deining commented Sep 3, 2024

It's a pity your repository is a fork that cannot be forked again. Therefore I'm unable to create a pull request that fixes your issue.

What I did: I created a new own repository which starts with javierasping/sentinel@0cf1c66 and has 3 additional commits

With the second commit, I added the change to config.yaml, as proposed by me:
deining/sentinel@7badf14

Then I regenerated the site via command hugo an d commited the changes in my third commit.
This third commit clearly shows that after changing config.yaml, google analytics script is now added to 115 different html pages in total:
deining/sentinel@060c295

For me this clearly shows that my change takes effect and effectively resolves your issue!

I hope that clarifies the situation and bring you on the right track!

@javierasping
Copy link
Author

It's true that with that change, the Analytics script has started to be automatically added, but what surprises me even more is that statistics are disabled (enabled: false) and yet it still works. Thank you so much for taking the time to help me! I'm going to change my config.yaml and remove the statistics I manually added.
image

Tonight, I'm going to apply the change to the page since I have a few posts where I need to remove the script that I manually added. Thank you so much for helping me, deining!!!

@javierasping
Copy link
Author

I have applied the commit to my repository. Thank you so much! I ran this script to manually remove the analytics code from all the individual posts where I added it, and the analytics are now working.

find . -type f -name "*.md" | while read -r fichero; do
  sed -i '/<!-- Google tag (gtag.js) -->/d' "$fichero"
  sed -i '/<script async src="https:\/\/www.googletagmanager.com\/gtag\/js?id=G-GVDYVWJLRH"><\/script>/d' "$fichero"
  sed -i '/<script>/d' "$fichero"
  sed -i '/window.dataLayer = window.dataLayer || \[\];/d' "$fichero"
  sed -i '/function gtag(){dataLayer.push(arguments);}/d' "$fichero"
  sed -i '/gtag('\''js'\'', new Date());/d' "$fichero"
  sed -i '/gtag('\''config'\'', '\''G-GVDYVWJLRH'\'');/d' "$fichero"
  sed -i '/<\/script>/d' "$fichero"
done

I credited you in the commit ;)) javierasping/sentinel@52dc44b

Thanks a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants