How to add analytics to your Fitbit app?
In this article, we describe how you can add basic analytics to your Fitbit app to track how many installs you have, how many monthly active users you have, etc.
Set Up Your Google Analytics Property
We assume that you already have a Google Analytics account set up.
- Go to the Admin page of your Google Analytics account and click on
+ Create Property. - Enter a name for your property, e.g.,
fitbit-app-tracking. - Click on
Show advanced optionsand make sureCreate a Universal Analytics propertyis enabled. Click onCreate a Universal Analytics property onlyfor simplicity. This will ensure that you can use thefitbit-google-analyticspackage for sending events to Google Analytics. - Complete the rest of the screen then click
Create. - Note down the tracking ID showed on the next screen (starts with
UA-).
Enable Analytics In Your App
We suggest using the fitbit-google-analytics npm package for emitting analytics events from your Fitbit OS app.
Installation & Permissions
Assuming you are using the Fitbit CLI in your workflow, you can simply install the aforementioned package via
npm install fitbit-google-analytics
Because the companion app needs to access the internet and needs to be run in the background, make sure you add the below permissions to your package.json file:
{
...
"fitbit": {
...
"requestedPermissions": [
"access_internet",
"run_background"
]
...
}
}
Code Changes
In order to enable fitbit-google-analytics, you just need to add the below snippets to your app and companion, making sure to enter the tracking ID you noted above:
App
import analytics from 'fitbit-google-analytics/app'
analytics.configure({
tracking_id: '<your-tracking-ID>'
})
Companion
import 'fitbit-google-analytics/companion'
In case you don’t have a companion for your app, you need to create one, as the companion is going to send measurement events to Google Analytics. For more details on how this works see below.
Emit Events From Your App
Out of the box, you will be able to track the number of active users and basic engagement with your app via fitbit-google-analytics (see full list here).
However, you probably want more insight into how your users are interacting with your app.
In the below sections we describe 2 specific use-cases.
Screen Views
In case you have multiple views in your app, you might want to track which ones are being looked at.
You can use the hit_type screenview for this:
analytics.send({
hit_type: 'screenview',
screen_name: 'Menu View'
})
Custom Interactions
For all other types of events, such as usage of specific settings, tapping on UI elements, etc., you can emit a hit_type of event:
analytics.send({
hit_type: 'event',
event_category: 'Game',
event_action: 'Start',
event_label: 'Arcade'
})
See the full guide for the events supported by fitbit-google-analytics here, including support of custom dimensions and metrics.
Testing & Troubleshooting
In order to test if you wired-up your app analytics properly, just launch your app in the emulator or on a Fitbit device and see if new events are showing up in the real-time overview in your Google Analytics account.
Debugging Events Sent
In order to debug what specific events are being actually sent and when, you may enable debug logging for fitbit-google-analytics. Unfortunately, the setting required for this is not exported, hence, you need to follow the below steps:
- Copy the source of
fitbit-google-analyticsinto your project’s root folder under thefitbit-google-analyticsdirectory. - Remove the dependency
fitbit-google-analyticsfrom yourpackage.json. - Turn on debug logging by setting the
debugvariable totruein both files in thefitbit-google-analyticsdirectory.
Background: From Users To Analytics Report
So how do events get sent from a Fitbit device?
fitbit-google-analytics works as follows:
- Upon first startup it generates a unique client ID to anonymously identify the Fitbit device.
- All events sent from the app are transferred to the companion via the file transfer API.
- The companion continuously forwards the events received from the app via HTTP calls using the Google Analytics Measurement Protocol v1 to Google Analytics.
Hence, keep in mind that events from a user may be ingested by Google Analytics only if that user has allowed access to the internet for your app and has no connectivity issues (otherwise events may not be sent at all or may be delayed).
References
- fitbit-google-analytics npm package.
- Chris Perko’s tutorial ‘Implementing Google Analytics’ on YouTube.