Example: Manually Track Pageviews with Sailthru JS and Google Tag Manager

This page offers an example implementation of the Sailthru.track() function, however, your requirements and preferred method for implementing the function may vary. For more information, see the function documentation.

This function can be used to manually track pageviews, rather than the default behavior of the JavaScript tag, which is to attribute a pageview to the current page URL when the page is loaded. Manual tracking–where you specify when to record a pageview, and what URL to log–is Ideal for single-page views, such as infinite-scroll content and other dynamic single-page sites. You can log a pageview using the Sailthru.track function at any time. This is typically used when autoTrackPageview is set to false in the Sailthru.init function. Note that if you leave autoTrackPageview set to true and use Sailthru.track, multiple pageviews will be logged. For instructions on how to configure the Sailthru.init function using Google Tag Manager, see the Introduction.

1. Add Sailthru.track Function as a Tag

  1. In Google Tag Manager, click New Tag, then change the tag’s name from “Untitled Tag” to “Sailthru.track”.
  2. Click Tag Configuration, then select the tag type Custom HTML, which allows HTML and/or JavaScript code.
  3. Copy the code sample below and paste it into the HTML box. We are manually logging a pageview to the URL http://example.com/test.
    <script type="text/javascript">
    
    Sailthru.track('pageview', { 
       "url": "https://example.com/test",
       onSuccess : function() {console.log('trackPageView success');}, 
       onError : function() {console.log('trackPageView failure');}
    });
    
    </script>
  4. Scroll down below the HTML field and click within the Triggering box to add a new trigger, so that Google Tag Manager knows when to fire the tag.

2. Trigger the Tag on Pageview

  1. After you click in the Triggering box, at the top-right, click grtm add trigger to add a new trigger.
  2. If you’re using a single-page, infinite scroll site you can create a trigger that fires on all pages.  Select Add Trigger and select All Pages.
  3. If you only want this to fire on a certain page, for example a page located at /products you can configure the trigger and Sailthru Track call to only fire on those pages.
  4. You can optionally give your new trigger a name in the top-left corner.
  5. Click Save.
The Sailthru track functionality puts a lot of control in your hands, and as an advanced user when combined with Google Tag Manager it can become a very useful tool to add.

Contact us

Top