<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hein Rutjes</title>
    <description>The latest articles on DEV Community by Hein Rutjes (@ijzerenhein).</description>
    <link>https://dev.to/ijzerenhein</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F359989%2F2f9a65ee-1b7f-4ef5-8815-f4c9e8fe0238.jpeg</url>
      <title>DEV Community: Hein Rutjes</title>
      <link>https://dev.to/ijzerenhein</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ijzerenhein"/>
    <language>en</language>
    <item>
      <title>Using Firebase Analytics with Expo
</title>
      <dc:creator>Hein Rutjes</dc:creator>
      <pubDate>Tue, 31 Mar 2020 22:19:35 +0000</pubDate>
      <link>https://dev.to/expo/using-firebase-analytics-with-expo-2ihp</link>
      <guid>https://dev.to/expo/using-firebase-analytics-with-expo-2ihp</guid>
      <description>&lt;p&gt;With the release of SDK 37.0.0, Expo now supports &lt;strong&gt;native Firebase Analytics 📈&lt;/strong&gt; in both Managed and Bare workflows. This release also includes some core enhancements  toward supporting more native Firebase services 🔥 in the future.&lt;/p&gt;

&lt;p&gt;Read on for instructions to get set up to record your first event, and more detail about how testing will work inside the Expo Client.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Installation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To get started, install both &lt;code&gt;expo-firebase-core&lt;/code&gt; and &lt;code&gt;expo-firebase-analytics&lt;/code&gt; into your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;expo &lt;span class="nb"&gt;install &lt;/span&gt;expo-firebase-core expo-firebase-analytics
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;code&gt;expo-firebase-core&lt;/code&gt; is another new package, it helps with automatically configuring Firebase on web and native.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Create a Firebase project&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you have done this already then you're good to go. Otherwise, &lt;a href="https://console.firebase.google.com/"&gt;go to the Firebase Console&lt;/a&gt; and create your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Configuration&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before you can start using Firebase Analytics, we'll need to add your Firebase project configuration to your Expo app. Firebase is configured using "Google Services" configuration files. We need to register iOS and Android apps to the Firebase project.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Register iOS and Android apps&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you haven't built a standalone app yet, now would be a good time to come up with a &lt;code&gt;bundleIdentifier&lt;/code&gt; for iOS and a &lt;code&gt;package&lt;/code&gt; for Android (&lt;a href="https://docs.expo.io/versions/latest/distribution/building-standalone-apps/#2-configure-appjson"&gt;more on that here&lt;/a&gt;) -  you will need these for your Firebase project configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Register a web app&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;It's important to also &lt;a href="https://firebase.google.com/docs/web/setup"&gt;register a web app&lt;/a&gt; with your project and keep the web configuration handy because we will need it in the next step. This is used to test Firebase Analytics in the Expo client app and when running your app on the web.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Add the Firebase configuration to app.json&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We need to now refer to this configuration on iOS, Android, and web inside of &lt;code&gt;app.json&lt;/code&gt;, as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"expo"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"ios"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"googleServicesFile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./GoogleService-Info.plist"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"android"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"googleServicesFile"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./google-services.json"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"web"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"config"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"firebase"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"apiKey"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"AIzaXXXXXXXX-xxxxxxxxxxxxxxxxxxx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"measurementId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"G-XXXXXXXXX"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Confused? Learn more in the detailed guide on how to setup native Firebase with Expo.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Basic usage&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once configured, you can now use &lt;code&gt;logEvent&lt;/code&gt; to record events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Analytics&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;expo-firebase-analytics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="nx"&gt;Analytics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;share&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;contentType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;itemId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Expo rocks!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;facebook&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Depending on the platform that you are on, it may take up to an hour for the event to show up in the Firebase Analytics console. &lt;a href="https://firebase.googleblog.com/2016/11/how-long-does-it-take-for-my-firebase-analytics-data-to-show-up.html"&gt;Read more about it here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can also associate additional user information with your events, so you can track specific audiences. Calling &lt;code&gt;setUserId&lt;/code&gt; and &lt;code&gt;setUserProperties&lt;/code&gt; will not record an event in itself, but transmit that information with every recorded event thereafter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Analytics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setUserId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;saitama&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;Analytics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setUserProperties&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;hero_class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Automatic event tracking&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Besides manual event logging, the native Firebase SDKs also tracks events automatically. Things like &lt;code&gt;first_visit&lt;/code&gt; and &lt;code&gt;first_open&lt;/code&gt; are recorded on startup of your app.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Recording navigation events&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In native apps, the native Firebase SDKs automatically track screen navigation. In React Native the navigation is often done in JavaScript and the Firebase SDK does not know how to hook-in to it, so you need to record these events yourself. The following example shows how you can use &lt;code&gt;setCurrentScreen&lt;/code&gt; to track screen navigation using react-navigation. See &lt;a href="https://reactnavigation.org/docs/screen-tracking/"&gt;Screen tracking for analytics&lt;/a&gt; in the React Navigation documentation, and where necessary substitute the tracking code with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Analytics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setCurrentScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="nx"&gt;currentRouteName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="nx"&gt;currentRouteName&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Usage with the Expo client&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are some differences when you use &lt;code&gt;expo-firebase-analytics&lt;/code&gt; with the App Store version of the Expo client, compared to in standalone builds and the Bare workflow.&lt;/p&gt;

&lt;p&gt;On standalone builds, in custom clients and in the Bare workflow, &lt;code&gt;expo-firebase-analytics&lt;/code&gt; uses the native Firebase SDK. In the App Store version of the Expo client, it uses a JavaScript implementation of Firebase Analytics. This is because native Firebase Analytics is designed as a singleton and is tightly coupled with the default Firebase app (&lt;a href="https://github.com/firebase/firebase-android-sdk/issues/1264"&gt;related issues&lt;/a&gt;), which doesn't work in the standard Expo client.&lt;/p&gt;

&lt;p&gt;It proved to be impossible to get &lt;em&gt;native Firebase Analytics&lt;/em&gt; working reliably in the Expo client, &lt;a href="https://github.com/firebase/firebase-js-sdk/issues/2644"&gt;so we created a custom Firebase Analytics JavaScript implementation that can communicate with the Google Analytics back-end&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This allows you to test your analytics inside of the Expo client, then have the full capabilities of the native SDK in your standalone app. Some automatic events are not recorded on the Expo client, but you can still record events using &lt;code&gt;logEvent&lt;/code&gt; and &lt;a href="https://docs.expo.io/versions/latest/sdk/firebase-analytics/?redirected"&gt;use all the functions of the API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order for the JavaScript implementation to work, don't forget to add the firebase web configuration to your &lt;code&gt;app.json&lt;/code&gt; as mentioned above.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The &lt;code&gt;measurementId&lt;/code&gt; field in the firebase-config is &lt;strong&gt;required&lt;/strong&gt;. If you don't see it then you might need to &lt;a href="https://firebase.googleblog.com/2019/07/firebase-google-analytics-upgrade.html"&gt;upgrade your Firebase project to the latest version of Google Analytics&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why not just include &lt;code&gt;react-native-firebase&lt;/code&gt; ?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A common question that we get a lot is why &lt;code&gt;react-native-firebase&lt;/code&gt; isn't included in the Expo client. If it were technically feasible for us to do that, we'd like to - but there are factors that prevent us from doing so.&lt;/p&gt;

&lt;p&gt;One of the problems is that some parts of the native Firebase SDK are written for single-app environments and are tightly coupled with other services. This makes running the native Firebase SDK in the multi-app Expo client challenging and, in some cases like Analytics, even impossible. Other Firebase services (like Dynamic Links and some Auth providers) require that URL schemes are defined within the app in order to work.&lt;/p&gt;

&lt;p&gt;Another issue for us is that &lt;code&gt;react-native-firebase&lt;/code&gt; doesn't support web or plan to. It's important for us that packages included in the Expo SDK either provide functional parity on web or, if not possible, then a reasonable fallback.&lt;/p&gt;

&lt;p&gt;Lastly, we need to minimize dependencies on external services in packages included in the Expo client that we ship to the stores. The core of the Expo SDK will be free of third-party service dependencies in the long run, and those libraries will instead be available to developers through our upcoming build service. This is still evolving, and we'll share more in the future.&lt;/p&gt;

&lt;p&gt;We are looking forward to bringing as much of the Firebase functionality to our users as possible. If you have Firebase-related feature requests, feel free to add or upvote them in our &lt;a href="https://expo.canny.io/feature-requests"&gt;feature request tracker&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Resources&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.expo.io/versions/latest/sdk/firebase-analytics/"&gt;&lt;code&gt;expo-firebase-analytics&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.expo.io/versions/latest/sdk/firebase-core/"&gt;&lt;code&gt;expo-firebase-core&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.expo.io/versions/latest/guides/setup-native-firebase/"&gt;Setup Native Firebase with Expo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
