Example: Manually Track Pageviews with Engage by 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
- In Google Tag Manager, click New Tag, then change the tag’s name from “Untitled Tag” to “Sailthru.track”.
- In Google Tag Manager, click New Tag, then change the tag’s name from “Untitled Tag” to “Sailthru.track”.
- In Google Tag Manager, click New Tag, then change the tag’s name from “Untitled Tag” to “Sailthru.track”.
- Click Tag Configuration, then select the tag type Custom HTML, which allows HTML and/or JavaScript code.
- 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>
- 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
- After you click in the Triggering box, at the top-right, click to add a new trigger.
- 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.
- 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.
- You can optionally give your new trigger a name in the top-left corner.
- Click Save.