Skip to main content

How to add the script to your site using Google Tag Manager

You can use "Google Tag Manager" to add Plausible Analytics tracking code to your website.

  • In your Google Tag Manager account, add a new tag
Add new tag
  • Edit the tag configuration
Choose tag type
  • Choose "Custom HTML" for the tag type
HTML tag
  • Paste your Plausible JavaScript tracking snippet in the "HTML" field within the "Custom HTML" section. Do note that Plausible will automatically detect that you're using Google Tag Manager and will present you a GTM-dedicated snippet that you need to use. We will display your snippet during the process of adding a new site to your account. If you've already added the site to your Plausible account, you can see the snippet within the "Site Installation" area of the "General section in your site settings.
Paste Plausible script
  • After pasting the snippet, add a trigger to make this tag fire
trigger to fire script
  • Select the "All Pages" and filter "Page View" as the trigger if you want to count all your pages
Tag trigger
note

Be careful about extras you enable such as tag firing priority, tag sequencing, consent checks etc as they all may affect when and how our script loads

  • Give your tag a name
Name tag
  • Click on the "Save" button

  • Click "Submit" to submit your changes

  • Then click on the "Publish" button

That's it! Now you can go to your website and verify whether Plausible Analytics script has been added and to your Plausible Analytics account to see whether the stats are being tracked. See here how to verify the integration.

Track 404 error pages

  • You can enable "404 error pages" as an optional measurement when adding a new site to your Plausible account. If the site has already been added to your account, you can control what data is collected in the "Site Installation" area of the "General section in your site settings. After you enable 404 error pages tracking, we will automatically add a new goal called 404 to your site
Enable 404 tracking during onboarding
  • The tracking snippet will change after your selection of "404 error pages" as an optional measurement. Do ensure to paste the newest snippet into your "Custom HTML" tag for all tracking to work as expected

  • Create a new GTM Variable

Create new GTM Variable
  • Name the new variable "Page Not Found" and set the type to "Custom JavaScript"
New GTM variable name and type
  • Create a new GTM Trigger
Create new GTM trigger
  • Name the new trigger "Page Not Found Trigger" and set the type to "Page View" and "DOM Ready"
New GTM trigger name and type
  • Add the following function to the trigger's field
    function() {
return typeof pageNotFound === 'undefined' ? 0 : pageNotFound;
}
New GTM trigger code
  • Make sure that the "Page Not Found Trigger" only fires on "Some DOM Ready Events" where "Page Not Found" variable is "greater than equal to 1"
New GTM trigger references
  • Add the following line within the body section of your website's 404 page template. For instance, if you're using WordPress, your 404 page template will be called 404.php and it will be located within your theme files
    <script type="text/javascript">
var pageNotFound = true;
</script>
  1. Create a new GTM Tag called "Page Not Found Tag" of type "Custom HTML" and paste the following code:
<script type="text/javascript">window.plausible("404", { props: { path: document.location.pathname } });</script>
  1. Publish all changes.

Track custom events

  • You can enable "Custom events" as an optional measurement when adding a new site to your Plausible account. If the site has already been added to your account, you can control what data is collected in the "Site Installation" area of the "General section in your site settings

  • The tracking snippet will change after your selection of "Custom events" as an optional measurement. Do ensure to paste the newest snippet into your "Custom HTML" tag for all tracking to work as expected

  • You then need to tag individual elements of your site by following our custom events guide.

Track custom properties

  • You can enable "Custom properties" as an optional measurement when adding a new site to your Plausible account. If the site has already been added to your account, you can control what data is collected in the "Site Installation" area of the "General section in your site settings

  • The tracking snippet will change after your selection of "Custom properties" as an optional measurement. Do ensure to paste the newest snippet into your "Custom HTML" tag for all tracking to work as expected

  • To track custom properties on the pageview level, you need to manually change your Plausible snippet to send the properties using the script.setAttribute

<script>
var script = document.createElement('script');
script.defer = true;
script.dataset.domain = "yourdomain.com";
script.dataset.api = "https://plausible.io/api/event";
script.src = "https://plausible.io/js/script.pageview-props.js";

script.setAttribute("event-author", "John Doe");
script.setAttribute("event-logged_in", "true");
// ... add a new line like above for each property

document.getElementsByTagName('head')[0].appendChild(script);
</script>

For more details on custom properties, do check out the custom properties guide.

Plausible containers for Google Tag Manager

If you prefer to get up and running with Plausible as quickly as possible, we have two Google Tag Manager containers that require minimal edits. Do right click the links below to download the linked files:

How to import Plausible's GTM containers

  • Navigate to the Admin section of your container's workspace
GTM admin
  • Click on Import Container
GTM import container
  • Select the downloaded .json file we provided and make sure to select merge
Select container and merge
  • If you chose the default tracking snippet container, make sure to edit the new Plausible Analytics tag and change yourdomain.com to the name of your dashboard
GTM container change domain
  • If you chose the full integration container that includes our optional enhanced measurements, make sure to change yourdomain.com to the name of your dashboard as well as to delete the script extensions that you don't need and to add the custom properties that you'd like to track as described above
GTM container change domain extensions and props