---
title: "How to add Klaviyo onsite tracking using Google Tag Manager"
description: "Learn how to add Klaviyo's onsite tracking snippets to your site using Google Tag Manager. Some site owners use Google Tag Manager as a single point to manage all third-party web snippets they need to add to their site. Klaviyo's onsite tracking falls into this category and can be added and managed through Google Tag Manager."
canonical_url: "https://help.klaviyo.com/hc/en-us/articles/360015392131"
markdown_url: "https://help.klaviyo.com/hc/en-us/articles/360015392131.md"
locale: "en-us"
category: "Analytics"
section: "Build and use metrics"
updated: "2026-10-29"
---

# How to add Klaviyo onsite tracking using Google Tag Manager

## You will learn

Learn how to add [Klaviyo's onsite tracking snippets](https://help.klaviyo.com/hc/en-us/articles/115005076767.md) to your site using [Google Tag Manager](https://marketingplatform.google.com/about/tag-manager/). Some site owners use Google Tag Manager as a single point to manage all third-party web snippets they need to add to their site. Klaviyo's onsite tracking falls into this category and can be added and managed through Google Tag Manager.

## Before you begin

**📝 Note:** If you're installing Klaviyo's onsite tracking snippets using Google Tag Manager and Shopify, please [review using Google Tag Manager on Shopify](https://help.klaviyo.com/hc/en-us/articles/360015392131.md#h_01HTFQ2G4BPPR9P1WHEQ1FWQR3).

## Setting up Google Tag Manager

If you have already created a Google Tag Manager account and installed its container on your site, you can skip these steps and head to the section on [creating a tag](#h_01GMKGSDWVDMW5DJF0RAQCWE2E).
1. Create a Google Tag Manager account.
2. Add the install scripts to your site.

![Inside Google Tag Manager, the two install scripts that will need to be copied into the head and body of your site's html](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/e69532391aec0203b26b7acb6619273d1010aa84-904x454.jpg?auto=format)

If you're new to using Google Tag Manager, [review their documentation on setting up an account and adding the install scripts to your site](https://support.google.com/tagmanager/answer/6103696).

## Creating a new tag

1. Within the workspace for your site, create a new tag for your Klaviyo *Active on Site* tracking. We recommend creating separate tags and triggers for your *Active on Site* and *Viewed Product* tracking. This is because *Viewed Product* tracking will only trigger on your product pages, whereas *Active on Site* tracking should trigger on every page of your site.
2. Select **Custom HTML** as the tag type.

![Within your site's workspace opening up the choose tag type menu and selecting custom html](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/57a8d263c74fcc63ac10312ea9fc5ef85f93f16e-1680x953.jpg?auto=format)

3. In Klaviyo, select **Integrations** in the sidebar.
4. Select **Manage data > Set up web tracking**.

If prompted, select **Set up manually**, then **Next** to view your snippet. If the setup screen shows instructions for your ecommerce integration instead, use the [manual Active on Site snippet](https://help.klaviyo.com/hc/en-us/articles/115005076767.md) and replace its public API key placeholder as directed.

![Inside of the integrations page in settings, the upper right button for setup web tracking is highlighted](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/8d3e797a6ceac7c7332cbd4208802ce161628a5a-1880x216.jpg?auto=format)
*5. Copy the first snippet.*

![Example of the web tracking snippet highlighted to copy](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/6fa6e280d434f56d50e2c148a56718efcbbc615b-656x340.jpg?auto=format)

6. From your Google Tag Manager, paste your Klaviyo *Active on Site* snippet into the HTML box.

![Active on site web tracking snippet pasted into HTML box in Google Tag Manager](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/bc21e8c0f8670d4e649634662b81af35a047a176-919x509.png?auto=format)

7. Set your trigger to fire on a page view for all pages. This ensures that your *Active on Site* snippet will fire whenever customers view a page on your site.

![Page view trigger example chosen as trigger for all site pages](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/0c7b67cd99306e606d0bbf5ca41dbc70429b9803-867x213.png?auto=format)

8. Save your new tag. [Preview your changes](https://support.google.com/tagmanager/answer/6107056) to check that the tag fires, then [publish your container](https://support.google.com/tagmanager/answer/6107163) to make tracking live on your website. Saving the tag alone does not publish it.

You can add your *Viewed Product* snippet using the same method. Use the snippet for your ecommerce platform from the [Viewed Product tracking guide](https://help.klaviyo.com/hc/en-us/articles/115005076767.md#h_01HADAYAAC3079D44928A58MTY). Modify the trigger to only fire on product detail pages. On some sites, this can be accomplished by limiting the trigger to only fire on pages where the URL includes the word "product". Check that your rule matches only product detail pages, then save, preview, and publish the tag.

![Example business website with /products highlighted in the URL](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/eb55d67b175118111c8a1e3fcbacc66c2b0ca45f-900x570.jpg?auto=format)

## Test your onsite tracking

1. Navigate to your website.
2. Add the following to the end of the URL:

`?utm_email=klaviyogreen@gmail.com`

Replace "klaviyogreen@gmail.com" with your own email address before loading the URL. If the URL already contains a `?`, append `&utm_email=your-email@example.com` instead. Place the parameter before any `#` fragment, then press Enter to reload the page.

3. In Klaviyo, navigate to **Analytics > Metrics**, select *Active on Site*, and open **Activity feed**. Look for an event associated with the email address you used in the URL.

![Inside the activity feed section showing profiles with active on website appearing indicating tracking is working ](https://cdn.sanity.io/images/6ct6b26e/help-center-prod/71bd618f073d4158e708a578515f114008f438de-1260x656.jpg?auto=format)

4. If you added *Viewed Product* tracking, visit a product detail page in the same browser. In Klaviyo, open **Analytics > Metrics**, select *Viewed Product*, and check its **Activity feed** for your test event and the product details.

If you have trouble getting your *Viewed Product* event to fire, make sure that your tags are set up to fire in a specific order. You always want your *Active on Site* snippet to fire before your *Viewed Product* snippet. Follow [Google’s tag sequencing instructions](https://support.google.com/tagmanager/answer/6238868) to set the order.

If Google Tag Manager reports an error when you save a snippet, check the code against [Google’s Custom HTML requirements](https://support.google.com/tagmanager/answer/6107167). If you cannot resolve the error, you can [install the snippets directly on your site](https://help.klaviyo.com/hc/en-us/articles/115005076767.md) instead.

## Using Google Tag Manager on Shopify

With Shopify, instead of using Google Tag Manager, you are encouraged to enable Klaviyo onsite tracking via [Klaviyo's app embed in Shopify](https://help.klaviyo.com/hc/en-us/articles/4425956184731.md). The app embed enables both *Active on Site* and *Viewed Product* tracking, given that the app embed is toggled on and that the **Add Viewed Product tracking to my site** setting is checked on the integration settings page. Our Shopify integration also enables *Submitted Search*, *Viewed Collection*, and *Added to Cart* via the **Track behavioral events** setting.

If you want to add onsite tracking via Google Tag Manager instead, make sure to toggle off the app embed first. Set up the *Active on Site* tag from the steps above to load Klaviyo.js on all pages. Then add the snippet below as a separate *Viewed Product* tag that fires only on product detail pages, after the base tag. This snippet does not load Klaviyo.js itself. Save, preview, and publish both tags, then complete both tracking tests above.

The Shopify product endpoint used below returns product tags, not collection membership, so the snippet captures `Tags` instead of collections.

```text
<script type="text/javascript"> 
//Initialize the Klaviyo object on page load
!function(){if(!window.klaviyo){window._klOnsite=window._klOnsite||[];try{window.klaviyo=new Proxy({},{get:function(n,i){return"push"===i?function(){var n;(n=window._klOnsite).push.apply(n,arguments)}:function(){for(var n=arguments.length,o=new Array(n),w=0;w<n;w++)o[w]=arguments[w];var t="function"==typeof o[o.length-1]?o.pop():void 0,e=new Promise((function(n){window._klOnsite.push([i].concat(o,[function(i){t&&t(i),n(i)}]))}));return e}}})}catch(n){window.klaviyo=window.klaviyo||[],window.klaviyo.push=function(){var n;(n=window._klOnsite).push.apply(n,arguments)}}}}(); </script>
<script type="text/javascript">
//Viewed Product
var klaviyo = window.klaviyo || [];
var product_handle = location.pathname.replace(/\/$/, '').split('/').pop();
fetch(window.Shopify.routes.root + 'products/' + product_handle + '.js').then(function(response){return response.json()})
.then(function(product){
  var item = {
    Name: product.title,
    ProductID: product.id,
    Tags: product.tags,
    ImageURL: "https:"+product.featured_image,
    URL: location.href.split( '/' )[0] + '//' + location.href.split( '/' )[2]+product.url,
    Brand: product.vendor,
    Price: product.price/100,
    CompareAtPrice: product.compare_at_price_max/100
  };
  klaviyo.track("Viewed Product", item);
  klaviyo.trackViewedItem({
    Title: item.Name,
    ItemId: item.ProductID,
    Tags: item.Tags,
    ImageUrl: item.ImageURL,
    Url: item.URL,
    Metadata: {
      Brand: item.Brand,
      Price: item.Price,
      CompareAtPrice: item.CompareAtPrice
    }
  });
// If you would like to add the Added to Cart snippet as well, place that here without its script tags
})
.catch(function(e){
  console.log('Klaviyo could not track Viewed Product. Please contact Klaviyo Support for assistance.')
});
</script>
```

**📝 Note:** Based on your Customer Privacy settings in Shopify, Klaviyo may not track onsite events for visitors to your Shopify store in the EU, EEA, UK and Switzerland, unless they have provided consent.

## Additional resources

- [Getting started with Klaviyo onsite tracking](https://help.klaviyo.com/hc/en-us/articles/115005076767.md)
  Learn about the different ways that Klaviyo can support onsite tracking on your ecommerce site. There are 2 key types of onsite tracking:
- [Understanding message conversion tracking](https://help.klaviyo.com/hc/en-us/articles/115005248128.md)
  Learn how conversion tracking works for email, SMS, and mobile push messaging and how to edit these settings. For each campaign and flow sent, Klaviyo automatically tracks conversions. This allows you to analyze the performance of your marketing channels and their individual success.
- [How to create a browse abandonment flow](https://help.klaviyo.com/hc/en-us/articles/115002775252.md)
  Learn how to create a browse abandonment flow to show shoppers products they viewed but didn’t purchase.
