seanlane / gochowdown

Hugo theme based on the Jekyll chowdown theme
MIT License
71 stars 51 forks source link

Google Analytics #9

Closed j450h1 closed 3 years ago

j450h1 commented 3 years ago

Is there a parameter to specify a Google Analytics property for tracking?

j450h1 commented 3 years ago

never mind i found it

j450h1 commented 3 years ago

Looks like adding my tracking id to googleAnalytics: in config.yml and then building the site didn't work. Can you confirm this is indeed a supported feature of this theme?

seanlane commented 3 years ago

Yeah, it is, I can see it present in the source code of my own instance of this theme: https://github.com/seanlane/recipes/blob/6597ffada57ffb0bfdb98042d5573d36304f463e/config.yml#L6

Within the source code, the Google Analytics script is then visible at recipes.lane.sh.

j450h1 commented 3 years ago

thanks @seanlane. That makes sense. I must have accidentally deleted something along the way in the theme.

j450h1 commented 3 years ago

Still struggling with this. I have set the tracking ID in config.yml, but still no luck. Can you point me to the file that reads this parameter and generates the appropriate html?

From recipes.lane.sh, I can see that it is added immediately below the footer:

</footer>

<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-31520487-7', 'auto');

    ga('send', 'pageview');
}
</script>

  </body>
</html>
seanlane commented 3 years ago

Sure, the file that generates the that script is https://github.com/seanlane/gochowdown/blob/master/layouts/_default/baseof.html#L40-L42

You can see that it's a conditional statement that adds that snippet of code if the googleAnalytics parameter in the config file is set.

When you set that parameter, is the snippet being inserted partially, but without the specified ID? Or is it not inserted at all? Also, which version of Hugo are you using to generate the site? Can you send the output of hugo version?

j450h1 commented 3 years ago

> blogdown::hugo_version() [1] ‘0.66.0’

It is not inserted at all. I went ahead and created a brand new blogdown site without any modifications and this is still the case.

j450h1 commented 3 years ago

I'm deploying using netlify which updates the site each time I update my github repo. Here are the logs - v0.66 is also used there: https://pastebin.com/tJtTiARF

j450h1 commented 3 years ago

Here is the test website I created - https://trusting-swanson-15b534.netlify.app/

Only 2 things I adjusted:

[build]
  publish = "public"
  command = "hugo"

[context.production.environment]
  HUGO_VERSION = "0.66.0"
  HUGO_ENV = "production"
  HUGO_ENABLEGITINFO = "true"

[context.branch-deploy.environment]
  HUGO_VERSION = "0.66.0"

[context.deploy-preview.environment]
  HUGO_VERSION = "0.66.0"
seanlane commented 3 years ago

I'm not sure why you're seeing those results. I opened an account to test with Netilify, and was able to get the tracking code: https://5fbdbb50b873e90007344525--goofy-mcnulty-6b476a.netlify.app/

The only thing I did (aside from fixing some minor, unrelated issues) was to add this config file: https://github.com/seanlane/recipes/blob/master/netlify.toml

The latest version of Hugo is 0.78, and that toml file uses it. Is there a reason you're specifying Hugo 0.66? Can you try with the toml file that I used (which I got from https://gohugo.io/hosting-and-deployment/hosting-on-netlify/) and see if that gives you anything different?

j450h1 commented 3 years ago

No particular reason why I was using v0.66. Just updated locally to latest v0.78 and added the netlify.toml file you mentioned.

No luck :( - Now I can't even see the images now: https://trusting-swanson-15b534.netlify.app

Here is the log file: https://pastebin.com/u8uwLErP

My best guess of the root cause is that:

A. The blogdown R package is doing something underneath the hood when I setup the RStudio project as per instructions here: https://github.com/rstudio/blogdown

B. There is some sort of conflict between the config files. I have three generated:

The third file is of course netlify.toml

[build]
publish = "public"
command = "hugo --gc --minify"

[context.production.environment]
HUGO_VERSION = "0.78.2"
HUGO_ENV = "production"
HUGO_ENABLEGITINFO = "true"

[context.split1]
command = "hugo --gc --minify --enableGitInfo"

[context.split1.environment]
HUGO_VERSION = "0.78.2"
HUGO_ENV = "production"

[context.deploy-preview]
command = "hugo --gc --minify --buildFuture -b $DEPLOY_PRIME_URL"

[context.deploy-preview.environment]
HUGO_VERSION = "0.78.2"

[context.branch-deploy]
command = "hugo --gc --minify -b $DEPLOY_PRIME_URL"

[context.branch-deploy.environment]
HUGO_VERSION = "0.78.2"

[context.next.environment]
HUGO_ENABLEGITINFO = "true"

My workflow is - I make changes in config. Enter blowdown::serve_site() to preview the website locally as it launches a hugo server. Then finally make the commit to push changes to GitHub. Then netlify builds the site.

Thanks for your assistance so far, but I think I'm going to have to move on to a different theme. Maybe I'll give it another try in if I encounter similar issues with other themes.

j450h1 commented 3 years ago

Okay, some good news! I decided to give it one final try.

I have no idea why, but deleting the config.toml file and renaming config.yml to config.yaml did the job (this 2nd part was due to a blowdown error message that said it couldn't find a config.toml or config.yaml file in the RStudio project). Anyways, thanks again for helping me troubleshoot.

seanlane commented 3 years ago

Interesting. Not sure why that fixed it, but glad to hear that it worked out! I'm going to close this issue since it seems resolved now, good luck with the site!