Where to add tracking code?

Where can I add tracking code within the element? This is not for Google Analytics, so I can’t use the config.toml file, unless I can use it in a way I do not know about. I would obviously prefer that to editing the theme file, so that I can easily switch themes. Thanks.

That depends on your theme. Some have a special template file where you can add code “right after the <body>-tag”, in some others you have to add the code by yourself.

  1. Post your repository here and we’ll have a look if there is an easy place.
  2. Where should the code go, into the <head>, after <body> opens or at the end of <body>? A good theme will offer these three locations for you.
1 Like

Thanks. I am currently using this theme: https://github.com/yursan9/manis-hugo-theme

I would prefer to put the tracking code within <head>.

use the code button for html-code. that’s this one; </> on top of this editor when you write a message. Then we can see that you might have written <head> there :wink:

Assuming you have installed the theme normally (meaning there is a folder themes/themename with that theme in your repo do the following:

  1. create a folder layouts in the ROOT DIRECTORY of your website (not the theme) (where the config.toml) is located.
  2. in there create a folder partials
  3. in there create a file meta.html
  4. in that file place the content of this: https://github.com/yursan9/manis-hugo-theme/blob/master/layouts/partials/meta.html
  5. then add your code before </head>

These are the rules:

  • Everything in your “local” layouts directory (the one you just created) will overwrite what happens in your theme. To change something from the theme, copy the file in question in the same place in your own layouts directory and Hugo will use that file instead of the theme file
  • If the theme gets an update you will have to check if the contents of the original changed or are the same. You might need to copy it over into your modification.
1 Like

Ah yes, all makes sense. Thank you.