Skip to content

Context is sometimes not complete #541

@cdedreuille

Description

@cdedreuille
  • analytics-react-native version: 2.2.0
  • Integrations versions (if used):
  • React Native version: 0.68
  • iOS or Android or both? iOS

Following this discussion #524 I'm trying to add a token in context.device.token but I still have very inconsistent behaviour. Sometimes the context will have all the data with the new token I just added but sometimes it will only have the token.

I tried to implement the new onContextLoaded() callback method but I might do something wrong.
Here is my code. Any chance you could tell me what I'm doing wrong?

import messaging from '@react-native-firebase/messaging';
import {
  Plugin,
  PluginType,
  SegmentEvent,
} from '@segment/analytics-react-native';
import type { SegmentClient } from '@segment/analytics-react-native/lib/typescript/src/analytics';
import * as Device from 'expo-device';

export default class CustomerIOPlugin extends Plugin {
  type = PluginType.before;

  configure(analytics: SegmentClient) {
    this.analytics = analytics;

    this.analytics.onContextLoaded(async () => {
      const token = await messaging().getToken();
      this.analytics?.context.set({
        device: {
          token,
        },
      });
    });
  }

  execute(event: SegmentEvent) {
    return event;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions