<head>, so the script behaves exactly as if you had pasted the tag into your HTML.
Add the tag
1
Copy the GTM snippet
In the dashboard, open your site’s Settings, select Tracking code and choose the Google Tag Manager tab. It has your public key filled in:
2
Create a Custom HTML tag
In your GTM container, create a new tag, choose Custom HTML as the tag type and paste the snippet into the HTML field.
3
Fire it on All Pages
Set the trigger to All Pages so the script loads on every page view. You only need one trigger: the script tracks single-page navigation by itself from the History API.
4
Publish the container
Submit and publish the container version, then open your live site. The status row on the Tracking code tab shows Receiving events. once the first event has been stored. Visits from
localhost and file:// are ignored, so GTM preview mode on a local site does not count.Add optional modules
The switches under Extra events on the Tracking code tab add onesetAttribute line per module to the GTM snippet. Each attribute is set with an empty value, which turns the module on. With outbound links, downloads, web vitals and errors on, the snippet looks like this:
s.setAttribute("data-exclude", "/admin/*");.
Why the attributes still work
The script reads its options fromdocument.currentScript, the <script> element that is running. A script element created with document.createElement and appended to the page is a classic script, so while it runs, document.currentScript points to that element and the script reads the data- attributes you set on it.
Two details in the snippet matter:
- Set the attributes before
appendChild. The script reads them once when it starts. - Use
setAttribute("data-…", …), not a property such ass.site. Onlydata-attributes are read.
https://in.crispstats.com, unless you set data-api.
If the CrispStats tag is also in your page HTML, the copy that runs second does nothing, so the visit is not counted twice.
Check that it works
- Open your live site with the browser’s developer tools on the Network tab. You should see
d.jsload fromin.crispstats.comand aPOSTtohttps://in.crispstats.com/ethat gets202. - In the Elements panel, find the
<script>element ford.jsin<head>and check that it carriesdata-siteand your other attributes. If they are missing, the tag sets them afterappendChild, or with properties instead ofsetAttribute. - Open
https://in.crispstats.com/check?k=cs_pk_xxxxxxxxwith your key."site": "known"means the key is right. The visit appears in Realtime within seconds.
How page changes are counted
The script counts the first load and then every URL change made through the History API, so single-page navigation is tracked without any extra trigger. Do not fire the CrispStats tag on a history change trigger as well: it would add a second script element, and while that copy does nothing, it is a wasted request. The same URL twice in a row is one pageview.Send custom events
Once the script has loaded, any tag can call the global. For a click you already have a GTM trigger for, add a second Custom HTML tag that fires on that trigger:data-cs-event attributes in your page’s HTML, with no GTM tag at all. See declarative events. A call made before the script has loaded is lost unless the queue stub runs first.
FAQ
Does a consent setting in GTM block CrispStats?
Does a consent setting in GTM block CrispStats?
GTM fires the tag according to the triggers and consent settings you give it. If the CrispStats tag waits for consent, visitors who do not consent are not counted. The script sets no cookie by default; see compliance when you decide. This is not legal advice.
Can I use the GTM tag and a script tag in the HTML?
Can I use the GTM tag and a script tag in the HTML?
Use one. If both run, the copy that runs second does nothing, so the visit is still counted once.
Can I load the script from my own domain through GTM?
Can I load the script from my own domain through GTM?
Yes. Set up a proxy, then set
s.src to your path’s d.js and add s.setAttribute("data-api", "/k3x9qe"); with your path.Next steps
Script attributes
Every option you can set with
setAttribute.Track custom events
Send actions with
crispstats() once the script is loaded.Troubleshooting
What to check when no events arrive.