How can I track a single-page application (manual method)?

Analytics + Tag Manager

Note: This method is only for accounts with versions below 16.19.0. In version 16.19.0 and higher, SPAs are tracked automatically

A traditional website (multi-page application) works in this way: every time a visitor goes from one page to another, the new page reloads in the visitor’s browser. An SPA (single-page application) loads at the beginning of the visit, then fetches the necessary data from the server as the visitor moves through the website, without the need for a page reload.

A multi-page application is easy to track because the tracking code loads each time a visitor visits a new page. A single-page application is more difficult to track because, in most cases, the tracking code loads just once, and doesn’t do so again as a visitor moves through the website.

To track a single-page application, we need a mechanism that detects when a visitor navigates to a new page within the SPA (typically when the browser’s history changes) and sends this data to Piwik PRO. To achieve this, we’ll use the following features:

  • Custom variable: This will provide us with information about the page title.
  • History change trigger: This feature will trigger the tag whenever there’s a change in the hash in the page’s URL or when the browser’s history object changes. It’s particularly handy for spotting changes that the tracking code might miss in single-page applications.
  • Virtual page view tag: This tag will capture and send information about the changes to Piwik PRO. Thanks to this, data about the page URL and page title will show up in the page report and the page title report.

Note: This method works for single-page applications where the page URL or the page URL fragment (aka hash) changes.

Set up tracking for a single-page application

To set up tracking for a single-page application, follow these steps:

  1. Go to Menu > Tag Manager.
  2. Navigate to Variables.
  3. Click Add a variable.
  4. Name the variable Document title and select the following type: Document.
  5. In Document name, type: title.
  6. When you’re done, click OK.
  7. Navigate to Tags.
  8. Click Add a tag.
  9. Name the tag and select the following type: Virtual page view.
  10. Click Next.
  11. Add:
    • URL: {{ Page Url }}
    • Document Title:  {{ Document title }}
  12. In Tag triggers, click Add a trigger.
  13. Name the trigger.
  14. In Event type, select History change trigger (SPA).
  15. Keep Wait 1 second before firing the tag turned on. It helps to match page URLs with page titles in single-page applications.
  16. (Optional) In Event conditions, adjust conditions. For more, see this article.
  17. Click OK.
  18. Click Save to save the tag.
  19. Test your tag in debug mode
  20. When you’re happy with how the tag works, click Publish.
  21. Voila!

Alternative setup for slow-loading sites

If your single-page application is slow to load the first page, the page titles for that page may be inaccurate. We recommend that you set up manual page view tracking for your single-page application instead.

To set up manual page tracking, follow these steps:

  1. Go to Tag Manager > Tags.
  2. On the left, choose the following tag: Piwik PRO (tracking code).
  3. Turn on Don’t track page views.
    Don't track page views in Piwik PRO
  4. Click Save.
  5. Navigate to Variables.
  6. Click Add a variable.
  7. Name the variable Document title and select the following type: Document.
    Document variable in Piwik PRO
  8. In Document name, type: title.
    Document variable in Piwik PRO
  9. When you’re done, click OK.
  10. Navigate to Tags.
  11. Click Add a tag.
  12. Name the tag and choose the following type: Custom code (async).
  13. Click Next.
  14. In Tag code, add the following code:
    <script>
    	setTimeout(function() {
      		_paq.push(['setCustomUrl', {{ Page Url }}]);
      		_paq.push(['setDocumentTitle', {{ Document title }}]);
     		_paq.push(['trackPageView']);
    	}, 1000);
    </script>

    Parameters

    1000
    The timeout value in milliseconds. Adjust it to the loading time of your site.

  15. In Tag triggers, click Add a trigger.
  16. Name the trigger.
  17. In Event type, select History change trigger (SPA).
    A history change trigger in Piwik PRO
  18. Keep Wait 1 second before firing the tag turned on. It helps to match page URLs with page titles in single-page applications.
  19. Click OK.
  20. Click Save to save the tag.
  21. Test your tag in debug mode
  22. When you’re happy with how the tag works, click Publish.
  23. All done!

View a report

Once the tag starts collecting data from your single-page application, you’ll see it in your reports. Each change will appear as a page view, complete with a page URL and title.

To view a report, follow these steps:

  1. Go to Menu > Analytics.
  2. Navigate to Reports.
  3. On the left, click Pages.
  4. Done! You can now view your report.

Was this article helpful?

Technical support

If you still have any questions, visit our community.
There’s always someone happy to help!

Related articles

Back to help center