How to create a custom viewed page metric
You will learn
Learn how to create a custom Viewed Page metric and understand when your customers visit non-product pages on your site.
Before you begin
Before continuing, make sure Klaviyo's onsite JavaScript (Klaviyo.js) is installed on each page you want to track. For Shopify, integrate your store and enable onsite tracking using the setup guide. For other sites, follow Getting started with Klaviyo onsite tracking. The snippets below require Klaviyo.js to send events to your account.
It's important to add the following JavaScript code snippet to each page of your site that you want to track a Viewed Page metric for. This snippet will work for both landing pages and any other site pages.
Installing the snippet
- Copy and paste the complete snippet below into each page you want to track. Before publishing, customize the page name and optional event name as described below, or use the alternate URL snippet.
- Then save and publish your website files with the updated code.
<script type="text/javascript">
//Script to initialize 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">
//Script to track viewed page
klaviyo.track("Viewed Page", {
PageName: "About Us"
});
</script>When an identified visitor views a page containing this snippet, Klaviyo records an event under your custom Viewed Page metric, with the PageName value "About Us".
Changing the PageName value
You can change the example value above to represent another page name on your site.
- Remove the example "About Us" value.
- Add your desired text contained within the quotation marks. For example, if you want to track visits to your brand stories page, you could instead use "Customer Stories" as the value.
Changing the Viewed Page event name
You can change the Viewed Page event name to another name (e.g., Visited Page, Tracked Page, etc.).
- Remove the example Viewed Page event name.
- Add your desired text contained within the quotation marks.
Track Viewed Page by URL
To track Viewed Page by URL instead of by page name, use the alternate snippet below in place of the first snippet. Keep window.location.href exactly as shown, without quotation marks. It automatically sets the url property to the URL of the page the visitor is viewing.
<script type="text/javascript">
//Script to initialize 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">
//Script to track viewed page
klaviyo.track("Viewed Page", {
url: window.location.href
});
</script>Test your viewed page tracking
To test if your Viewed Page tracking is working, make sure your page has been published. Add ?utm_email=youremail@example.com to the page URL, replacing youremail@example.com with your own test email address. If the URL already contains a ?, use &utm_email=youremail@example.com instead. Add the parameter before any # fragment, then reload the page to identify your browser and track your visit.
- After visiting your page, go into your Klaviyo account.
- Navigate to Analytics > Metrics.
- Search for Viewed Page (or, whatever alternative name you use for this metric) in the field above.

4. Click into your Viewed Page metric (or whatever metric you are using).
5. Select the Activity feed tab. Find the event for your test profile. Open its menu and select Activity details to check that the page name or URL matches the page you visited.

If you can view your activity, that means the metric is tracking successfully. If you don’t see your activity, try reloading the page in Klaviyo. If it still does not appear, confirm that Klaviyo.js and your chosen snippet are installed on the published page, then revisit it with your test email address. For Shopify, also check the consent requirement below.
For Shopify stores, 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
- Understanding cookies in Klaviyo
Learn more about how Klaviyo uses cookies as a part of our web tracking to gather information and help improve conversion rates and email performance. This article explains the specific cookie we use and its purposes so that you understand how customers are tracked. This information helps you understand how Klaviyo gathers data, and how this may impact customer privacy and compliance laws.
- Getting started with Klaviyo onsite tracking
Learn about the different ways that Klaviyo can support onsite tracking on your ecommerce site. There are 2 key types of onsite tracking:
- Setting Up API-based Website Activity Events