2

I am trying to integrate Amplitude to my React Native project. I am currently still developing the application and using Expo. The first event I am trying to capture is when a user is logged in.

const events = {
  USER_LOGGED_IN: 'USER_LOGGED_IN',
  USER_CREATED_ACCOUNT: 'USER_CREATED_ACCOUNT',
};
let isInitialized = false;
const apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const initialize = () => {
  if (!Environment.isProduction || !apiKey) {
    return;
  }

  Amplitude.initialize(apiKey);
  isInitialized = true;
};

In my render function (above the return) I have this line of code:

render() {
    Expo.Amplitude.logEvent('USER_LOGGED_IN')
return (

I am not seeing any events coming into amplitude. Is it possible to see events while using expo to run my code?

Note- this code is in my home screen component

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Emil Juboori
  • 199
  • 2
  • 4
  • 12
  • 3
    I would recommend you put this logic in something like `componentDidMount` –  Dec 11 '17 at 22:54
  • unfortunately not seeing anything still @EdgarAroutiounian . Have you integrated amplitude into a project before? – Emil Juboori Dec 12 '17 at 00:31
  • not in my own personal apps but example source code is here: https://github.com/expo/expo/blob/master/js/Api/Analytics.js –  Dec 12 '17 at 13:40

3 Answers3

3

You need to publish your Expo app to see the events on Amplitude because the integration works only on prod env. Once your app is published, you'll see the events on Amplitude dashboard with a small delay, usually 1 minute.

Francesco Fiori
  • 183
  • 2
  • 7
2

This is what I did for amplitude to work

expo install expo-analytics-amplitude

Analytics.js

import * as Amplitude from 'expo-analytics-amplitude'

let isInitialized = false
const apiKey = 'YOUR_KEY_HERE'

export const events = {
    HOME: 'HOME'
}

export function initialize() {
    if (isInitialized || !apiKey) {
        return
    }

    Amplitude.initialize(apiKey)
    isInitialized = true
}

export function track(event, options) {
    initialize()

    if (options) {
        Amplitude.logEventWithProperties(event, options)
    } else {
        Amplitude.logEvent(event)
    }
}

export default {
    events,
    initialize,
    track
}

Import in the file where you need tracking

import Analytics from '../auth/Analytics'

...

useEffect(() => {
  Analytics.track(Analytics.events.HOME)
}, [])
Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
1

Expanding on the code above, I made a few minor updates. I will update this if I find a better way to fully integrate.

expo install expo-analytics-amplitude
import * as Amplitude from 'expo-analytics-amplitude'

let isInitialized = false
const apiKey = 'your API key'

export const events = {
    HOME: 'HOME'
}

export function initialize() {
    if (isInitialized || !apiKey) {
        return
    }

    Amplitude.initializeAsync(apiKey)
    isInitialized = true
}

export function track(event, options) {
    initialize()

    if (options) {
        Amplitude.logEventWithPropertiesAsync(event, options)
    } else {
        Amplitude.logEventAsync(event)
    }
}

export default {
    events,
    initialize,
    track
}

Import into the file you need tracking. I initialized my connection to Amplitude in App.js.

  import Analytics from "./app/auth/Analytics"; 

  useEffect(() => {
   Analytics.initialize()
   Analytics.track(Analytics.events.HOME)
  }, []);
katann0401
  • 11
  • 2