How to track usage of a LiveView app -- Google Analytics?

I’m wondering, perhaps I could track “page loads” in Google Analytics using hooks like on_mount, but mounted seems too limited, and beforeUpdate seems too general (it would fire even if the URL did not change, although perhaps I could check for that?)

Is there some other better way to track usage of a LiveView app?
Thanks!

1 Like

This is how I tackled the same issue but with analytics.js.
I think the phx:page-loading-stop event listener is the place to track live redirects/patches.

window.addEventListener("phx:page-loading-stop", (info) => {
  //hides loading bar
  topbar.hide();
  // liveredirects/livepatches tracked as a page view
  if (["redirect", "patch"].includes(info.detail.kind)) {
    window.analytics.page();
  }
});
1 Like

As LiveView uses browser’s pushState API, it is possible to use History Change Trigger in Google Tag Manager. Used approach might depend a bit, from which place, you want to control tracking in general. I was able to get an event in Google Analytics, whenever a path changes on LiveView page.