Google Analytics IOS

first intall @react-native/firebase-analytics

npm install @react-native-firebase/analytics

and then pod install

cd ios/ && pod install

in folder Utils create GoogleAnalytics.js

import analytics from '@react-native-firebase/analytics'
import { Platform } from 'react-native'


//login tracking
export const loginAnalytics = async () => {

  try {
    await analytics().logLogin({
      method: Platform.OS === 'ios' ? 'ios' : 'android',
    })
  } catch (error) {
    console.log(error, 'error login')
  }
}

//add to cart tracking
export const addToCartsAnalytics = async () => {
  try {
    await analytics().logAddToCart({
      currency: 'USD',
      items:[
        {item_id: '1', item_name:'baju kokoh', price: 10000, quantity: 2}, 
        {item_id: '2', item_name:'lingerie', price: 10000, quantity: 2}
      ],
      value: 200000
    })
  } catch (error) {
    console.log(error)
  }
}

//custom event tracking => this bicket custom event
export const logEvent = async () => {
  try {
    await analytics().logEvent('bicket', {
      id: 12312312,
      item: 'Baju Baru',
      description: ['round neck', 'longSelved'],
      size: 'L',
    })
  } catch (error) {
    console.log(error)
  }
}

Example for call the function utils

import react from 'react'
import { logEvent } from '../Utils/GoogleAnalytics'

function App() {

  const handleItemGoogle = async () => {
    logEvent()
  }


return(
      <Button onPress={() => handleItemGoogle()}>custom event</Button>
)
}

export default App

how to track this Google Analytics is working ?

open your xcode and then set your aurguments scheme

When running on iOS in debug, events won't be logged by default. If you want to see events in DebugView in the Firebase Console when running debug builds, you'll need to first set a flag when launching in debug. This flag used to be variously called -FIRAnalyticsDebugEnabled and -FIRDebugEnabled, but please check the previous link.

To enable Analytics debug mode on your development device, specify the following command line argument in Xcode:

-FIRDebugEnabled

This behavior persists until you explicitly disable debug mode by specifying the following command line argument:

-FIRDebugDisabled

and then click https://console.firebase.google.com/project/_/analytics/debugview for tracking firebase function is working

Last updated

Was this helpful?