← Back to Trustlytics Sign in
Support Center Settings Tracking code Add the tracking code to an HTML document

Add the tracking code to an HTML document

Built your website in HTML? Then installing it is quick work. You open your file, put the code in a specific spot, save, publish. Done.

This guide is also your safety net if your system is not on our list. The route through the HTML works anywhere you can get at the source code.

Video or guide. Take whichever suits you:

The video is in the works. Until then, the guide will get you there.

Step-by-step guide

What you need for this

  • Your tracking code. You get it under Copy the tracking code with one click on "Copy".
  • Access to your HTML files. Either directly on your computer or through your hosting provider's file manager.
  • An editor. Visual Studio Code, Sublime Text, Notepad++, or whatever you are used to. In a pinch, your operating system's plain text editor will do.
A word about Word: never open HTML files with a word processor like Word. It inserts invisible formatting when saving and breaks your file. Use a real code editor.

Opening the HTML file

Open your HTML file in the editor. The home page is usually called index.html.

Working directly on the server? Then sign in with your hosting provider, open the file manager, right-click the file, and choose "Edit".

Finding the head section

Every HTML document has two sections:

Section What is in it
<head> The head section. This holds information the visitor does not see: page title, fonts, references to your design. And this is where the tracking code belongs.
<body> The content section. Everything your visitors actually lay eyes on: text, images, menus.

<!DOCTYPE html>
<html lang="en">

<head>
  <title>TITLE</title>
  ...
</head>

Now you look for the closing </head>. The slash matters: <head> opens the section, </head> closes it again.

Do not feel like searching? Press Ctrl + F (Windows) or Cmd + F (Mac) in your editor and type </head>. The editor jumps straight to the right spot.

Inserting the tracking code

Put your cursor on the line directly above </head> and paste your tracking code there.

Here is what the result looks like:


<!DOCTYPE html>
<html lang="en">

<head>
  <title>TITLE</title>
  ...
  <!-- Trustlytics -->
  <script defer data-domain="YOUR-DOMAIN.COM" src="https://app.trustlytics.ch/tl.js"></script>
  <!-- End Trustlytics Code -->
</head>

Important: you do not have to type anything here. Your domain is already in your code, filled in and ready. The YOUR-DOMAIN.COM above is just a placeholder so you can see how the code is built. Always get the real code with the "Copy" button under Tracking code.

Why right before </head>? Because the code then loads early, before your content appears. That way every visit gets counted reliably, even when someone leaves the page right away. Put it further down and you lose the short visits.

On every page (not just the home page)

Here is the spot where most people trip up. The code has to go into every single HTML file you want to measure. So into index.html, but also into contact.html, about-us.html, and all the rest.

If it is missing on one page, that page simply does not show up in your reports. And because the other pages are delivering numbers, you often notice the gap late.

Working with a shared header file? Some editors and site builders let you create a section once and include it on every page. If that is your setup, one entry there is enough. The code then shows up everywhere automatically, including on pages you create later. With plain HTML and no templates like that, there is no way around it: every file individually.

Saving and publishing

Save every file you changed. Then upload them to your server, exactly the way you normally do. Over FTP, through your hosting provider's file manager, or whatever your usual route looks like.

Do not forget: as long as the changed file only sits on your computer, nothing happens. Trustlytics only counts once it is online.

Checking

Now the moment of truth, and it takes less than two minutes:

  1. Open your website in a new browser window.
  2. Click through a few pages.
  3. Switch to the Real-time report in Trustlytics.
  4. Within a few seconds, your own visit should show up there.

If something is there, everything is installed correctly. If it stays empty, the section below will help.

When nothing gets counted

Four causes cover almost every case:

Cause What to do
File not uploaded Open your website and look at the source (right-click, "View page source"). If the code is not in there, the changed file is still sitting with you.
Code incomplete Selecting by hand, and a character is quickly lost. Get the code under Tracking code with the "Copy" button and paste it in again.
Wrong address registered The code only works on the website listed in your account. Check that under Website.
Cache Your browser or your hosting provider is still serving the old version. Reload while holding Shift, or clear your hosting cache.

Common questions

Does the code really have to go in every file?
Yes, into every page you want to measure. With plain HTML there is no shortcut. If you use an editor that works with shared templates, one entry in the template is enough.

Can I also put it in the <body>?
Technically it would usually work there too, but we advise against it. In the <head> it loads earlier, and short visits do not get lost.

What if I already have other scripts in the <head>?
No problem. Just add the Trustlytics code alongside them, directly before </head>. The scripts do not interfere with each other.

I cannot find </head> in my file. Now what?
Then you may not have opened the main file but a partial one. Check whether the file starts with <!DOCTYPE html>. If that is missing too, this file belongs to a larger structure. In that case, look for the file that contains <html> and <head>.

Do I have to reinstall the code when I change a page?
No, as long as you do not delete the code by accident. With a new page you do have to add it, otherwise that page will not be counted.

Will my website get slower because of it?
No. The script is tiny and loads in the background without holding up the rest of your page.

Was this article helpful?