How to Track Pageviews in Single Page Application?

A Single Page Application is a web-based application that tries to fit on a single page to provide a desktop-like application to its users. Single page apps have the unique characteristics to redraw any component of the UI without requiring a server to round trip to recover HTML.

Examples:

  1. Ecommerce pages loading content dynamically
  2. Faster blog reading experience
  3. New website, updating content in background

Single-page apps are very fast and responsive compared to traditional webpages. It also saves web resources.

Tracking page view helps to get the data regarding the number of views a particular page has got during a predefined period. Single Page Apps (SPA) are great to use for different reasons, but they require a bit more expertise to set up tracking page views than required for a traditional page.

 

For the traditional sites, the javascript tracking snippet is perfectly applicable, but for a single page application where the new page content loads dynamically not full page loads like traditional websites, the “analytics.js” snippet only runs once. This implies that the virtual pageviews need to be tracked manually when the new content loads.

Here are two processes of tracking page view for Single Page Applications:

Tracking dynamically loaded content as distinct page views, a page view hit can be sent to Google Analytics by specifying the document path and setting the “page” field. It symbolizes only the portion of the path in a URL and needs to start with a slash. Normally, the page value and the path portion of the given URL are found to be same, but this is not mandatory. It is found that websites that load content dynamically normally updates the hash portion of the URL and in this context several different URL paths may point to the same resource.

Setting Page Data for Multiple Hits:

It is obvious that specifying a page value in a page view sends the page value to the Google analytics for that page only. It doesn’t automatically update the stored page value on the tracker. This is problematic in some situations particularly when other “hits” are sent and don’t clearly contain the current page value. In this situation, Google analytics associates those hits with only those URLs present at the time of preliminary webpage load. This issue can be easily avoided by periodically updating the tracker object with all new information before sending the hits.

Two things to be noted while tracking page view for Single Page Application:

  • The document location needs no updating
  • The document referrer needs no updating
  • New trackers should not be created

Summary:

if your webpage is very slow, there is an urgent need to speed it up. Converting them to a single-page application will be very useful. If you are looking for assistance in setting single page application for your business page, use our web development services.