lxndrblz / anatole

Anatole is a minimalistic two-column theme for Hugo.
https://themes.gohugo.io/themes/anatole/
MIT License
651 stars 363 forks source link

Google Analytics v4 requires different internal Template than currently used #339

Closed vladimirdoroch closed 2 years ago

vladimirdoroch commented 2 years ago

Describe the bug GA client script does not send proper analytics metrics to Google. This happens if one uses the anatole template and sets a Google Analytics v4 tracking Id in the config file.

image

As shown in the picture, a client side script is run but does not send any metrics via Post ("Kein Inhalt für diese Anfrage" = "No content for request").

To Reproduce

  1. Go to config.toml
  2. Set googleAnalytics = 'G-MEASUREMENT_ID'
  3. Start hugo server hugo server -D
  4. Start a Webbrowser of choice and browse to lokal instance
  5. Check POST messages to google-analytics.com via Webdev Tools of your Browser

Expected behavior According to Hugo Docs setting a GA v4 ID is sufficient to configure the internal template properly.

The client side GA script should transmit some metrics to Google via POST messages.

image

In the example above:

en=page_view&ep.anonymize_ip=false
en=page_view&_et=1&ep.anonymize_ip=false
en=user_engagement&_et=4818&ep.anonymize_ip=false

Desktop (please complete the following information):

Additional context My current work around is a custom partial for footer.html which uses _internal/google_analytics.html template instead of _internal/google_analytics_async.html as specified in Hugo Docs.

lxndrblz commented 2 years ago

@vladimirdoroch If I read the docs correctly, we would need to distinguish between a GAv4 and the v3. In case of the former we should load google_analytics.html and not _internal/google_analytics_async.html. Right now, I am not sure how this distinction could be made programmatically.

vladimirdoroch commented 2 years ago

Maybe one can load the appropriate template based on the prefix of the tracking Id. As I understand v3 (UA-XXXXXXX-X) and v4 (G-XXXXXXXX) tracking Ids can by identified that way.

This could be catched by something like {{ if hasPrefix . "G-"}}. What do you think?

lxndrblz commented 2 years ago

@vladimirdoroch That seems like a smart solution. Feel free to send a PR and I will go ahead and merge it for you.

vladimirdoroch commented 2 years ago

Created pull request #341