Analytics

How to connect your app to an analytics platform

Because your app running on the Senza platform is just like any other web app, it's easy to capture analytics using industry standard tools such as Google Analytics (GA4). If you're using GA4 to capture page views on your corporate website, you can use exactly the same techniques to track activity in your Senza app.

Similarly, if you're using an existing analytics package to track video playback in a player such as Shaka, it should also work straight out of the box with minimal modification. Here are a few things to keep in mind.

User identity

When tracking your users, you want to know who they are. This can be accomplished in a number of ways. If users sign into your app, for example using Device Authentication or QR Code Authentication, you can include their user or household ID from your platform in the user properties.

Every device on the Senza platform has a unique device ID. If you have a database that stores which devices were given to which users, you can call an API to look up the user info and then send it with the analytics data. Alternatively, you can send the device ID with the analytics data and then correlate it with your user database in a downstream system.

Geolocation

Typically, analytics packages track user location by geolocating the IP address of incoming connections. Because your Senza app runs in a browser in the cloud, GA4 will think that all your users live in data centers. To work around this, you can geolocate the user in your app by accessing the actual client IP address from the Device Manager, and then include the location information (i.e. city, region, country) in the user properties. See the Geolocation tutorial for more details.

Note that it is against GA4 policies to log personally identifiable information, including IP address or precise location (street address or latitude and longitude). Less precise location information at the neighborhood level is allowed.

Lifecycle

As your app runs, it may switch between different Lifecycle states: foreground as the user is interacting with your app, background when they have not interacted for a few moments, and suspended when they are consuming content or not actively engaged.

When the app is in suspended mode, the JavaScript engine shuts down so your app cannot send analytics events—which is fine, since the user isn't actively doing anything anyway. When the user presses a button and the app starts up again, you'll want to restore the state from session storage without starting a new analytics session. See Preserving State for more details.

Disconnect

On most platforms, if you wanted to measure how long the viewer watched a piece of content, you would need to periodically send heartbeat events to tell the analytics engine what is happening. If the viewer closed the browser or turns off their device, the program goes away, so you don't know what happened towards the end of the session.

Because of the cloud-based architecture of the Senza platform, it offers a unique feature: the platform can notify your app that the device has disconnected from the service. This event is guaranteed to be delivered, even if the user pulls the power cable from the device. This gives you an opportunity to send a summary analytics message at the end of the session. See the Lifecycle Events guide for more details.

Sample Code

The sample code includes an analytics module that demonstrates all of the techniques above. It can send basic events, track all activity in the local player and the remote player, track lifecycle states, and more. See the Analytics tutorial for a complete walkthrough of the functionality. This is just an example; you can adapt these techniques as needed for your app and the analytics platform of your choice.