# Add analytics to your Phoenix Live View app

_It can be difficult to know where to put analytics tracking code on your Phoenix Live View web application. Snippets like Google Analytics don't work with Live Views due to the lack of page refreshes._

Recently I was trying to add Go Squared (a Google Analytics competitor) to our Phoenix Live View application and realised it wasn’t going to work due to the lack of page reloading.

Usually, the analytics snippet does something like this:

Live views don’t make a normal HTTP request (they use websockets) and thus when a user goes from one live route to another the analytics javascript snippet doesn’t run.

Instead, we need to hook into the live view lifecycle. Create a new file next to your `app.js` file: `analytics.js`.

In your app.js:

Then in you analytics.js:

This mostly works, except I found when doing a live redirect the `trackPageView()` function was running twice! I have no idea if it was something to do with my app or a live view issue. To fix, I put the last page into a variable, and be sure not to double up with the page views:

Now it only gets called once per page!

Article [source](https://petal.build/blog/Add-analytics-to-your-Phoenix-Live-View-app)

---

Written by Joe Corcoran for [Clarvelo](https://clarvelo.com).

Clarvelo helps teams run structured retros, decision jams, post-mortems, and other key decision-making workshops with an AI facilitator. Assigned outcomes are automatically exported to wherever your team works.

[Read more on the Clarvelo blog](https://clarvelo.com/blog) or [try the live AI-facilitated workshops](https://clarvelo.com).
