I am trying to track simple events and pages/views through Firebase analytics.
I have a progressive web app, (SPA). It's a fullscreen game and it is not using browser history.
I am a bit confused by Google's docs to be honest, sometimes they only explain how it works in ios/Android and not in web. Also, Firebase analytics are different than the "normal Google analytics".
Firebase analytics API hasn't much:
analytics().logEvent()
analytics().setAnalyticsCollectionEnabled()
analytics().setCurrentScreen()
analytics().setUserId()
analytics().setUserProperties()
While the "Google Analytics docs" do have a section about SPA's I haven't found how to do the same with Firebase analytics, such as manually sending page views that would be like:
ga('set', 'page', '/new-page.html');
ga('send', 'pageview');
I have tried to use setCurrentScreen
but it does nothing.
Help appreciated.
Just add the Firebase SDK to your new or existing app, and data collection begins automatically. You can view analytics data in the Firebase console within hours. You can use Analytics to log custom events that make sense for your app, like E-Commerce purchases or achievements.
Analytics surfaces data about user behavior in your iOS and Android apps, enabling you to make better decisions about your product and marketing optimization. View crash data, notification effectiveness, deep-link performance, in-app purchase data, and more.
The new model. Recent updates to the Firebase SDK (6.29. 0 on iOS and 17.5. 0 on Android) allow you to disable automatic screen tracking and manually log the screen_view event when it makes sense, regardless of your app's design. And you can even add custom parameters to the event!
I also wasn't able to find one place with all this information, so here is how I got it to work.
If you are using npm
, Make sure to import the analytics:
import * as firebase from "firebase/app";
import "firebase/analytics";
const analytics = firebase.analytics;
Analytics automatically come with the latest Firebase SDK.
Make sure that "Google Analytics" is enabled in Firebase Project Settings (Integrations tab). Also, make sure that Firebase Config has measurmentId: "ID_HERE"
specified in the code - this value can also be found in Firebase Project Settings (General tab).
analytics().setCurrentScreen(window.location.pathname) // sets `screen_name` parameter
analytics().logEvent('screen_view') // log event with `screen_name` parameter attached
The "page_view"
event seems to be logged automatically by default. I don't know why, but you can use the count of that event to track how many people reload/visit the app.
Relevant Links:
React is quite common, so I'll also list my solution for logging using it. Place the <AnalyticsComponent />
between <BrowserRouter>
declaration.
const logCurrentPage = () => {
analytics().setCurrentScreen(window.location.pathname)
analytics().logEvent('screen_view')
};
const AnalyticsComponent = () => {
const history = useHistory();
useEffect(() => {
logCurrentPage(); // log the first page visit
history.listen(() => {
logCurrentPage();
});
}, [history]);
return (<div></div>);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With