Tracking Page Views
Freestar collects page URLs to track page views each time pubfig.min.js is loaded. However, in a Single-Page Application (SPA), the DOM and/or window lifecycle does not reload Freestar's script (pubfig.min.js) each time the URL is updated.
For SPAs - there are two pageview tracking methods available, Automatic and Manual.
Automatic pageview tracking will be initiated by Freestar when there is a change in the URL through history.pushState or history.replaceState. If we detect a URL change through the Browser History API, we automatically send pageview data.
Manual pageview tracking allows publishers to control Freestar's pageview tracking feature and choose when to send pageview information. Publishers utilizing Manual pageview tracking will use the freestar.trackPageview() method to ensure accurate data collection.
To implement Manual pageview tracking on your SPA, follow the instructions below.
- Insert the following line into Freestar's head code to disable Automatic tracking of pageviews.
freestar.disableAutoTrackPageviews = true;
-
Whenever the URL changes, you can then invoke freestar.trackPageview as shown below.
freestar.queue.push(function(){
freestar.trackPageview()
}); - Optionally, Publisher's can pass the path argument with the current URL to the freestar.trackPageview function. This URL will be stored in the database. If they do not provide this, we will use the fallback
window.location.href.freestar.queue.push(function(){
freestar.trackPageview({ path: https://example.com })
});
Lastly, as mentioned earlier, we automatically send pageview information when the page first loads. Therefore, publishers only need to call the freestar.trackPageview(); function when the URL changes.
Please speak with your Freestar Point of Contact before implementing the trackPageview function.
The number of tracked pageviews for the current session can checked in the browser console. To get this run the following line in the console.
Number(sessionStorage.getItem('pageviews'))