Track route change for a SPA

Hi,
I am looking for a solution to track route change in a single page application. I know there is an event “trackPageView”. trackPageView gets trigger only once on app load. I know there is link tracking event as well but that only works when the anchor tag inside of link has a “href”. If say the route of the application is changing via javascript and achor tag has a click event to trigger that then link tracking is not triggering an event for that.

<li><a onclick="goToCart()"></i>Cart</a> </li>
<li><a href="/cart"></i>Cart</a> </li>

So link tracking event will trigger only for second link which has a href.
Question-1 - So is there a way that the link tracking can happen for first link as well without manually calling trackLinkClick function?

In addition I want to track when a single page application route from one page to another
Question-2 - Do you have any best solution for that?

@downtoearth, you can approach it from 2 angles:

  1. Manually trigger trackPageView for each virtual page view
  2. Manually trigger trackLinkClick as part of goToCart() function
1 Like

This is the best option (as if something doesn’t have an href it’s technically not a link) but you can do something like:

<li><a href='web+snowplow://123' onclick="goToCart()"></i>Cart</a> </li>

if you really want to avoid manually trigger a link click for some reason.

1 Like

Thank you @ihor

Thank you @mike