analytics-react-native icon indicating copy to clipboard operation
analytics-react-native copied to clipboard

analytics-react-native-plugin-adjust not compatible with react-native-adjust v5

Open kathaypacific opened this issue 1 year ago • 1 comments

Hello! This is both a bug report and feature request to support react-native-adjust v5, which was released about 3 months ago. It seems that the latest version of @segment/analytics-react-native-plugin-adjust (v0.7.0) is not compatible with the api changes that were made in react-native-adjust v5, causing the Segment client to fail to initialise altogether.

  • analytics-react-native version: 2.20.3
  • Integrations versions (if used):
  • React Native version: 0.72.15
  • iOS or Android or both? both

Steps to reproduce

  1. run yarn add @segment/analytics-react-native-plugin-adjust react-native-adjust as in the segment installation instructions
  2. try to run your app after executing the rest of the segment installation instructions

Expected behavior No errors

Actual behavior The segment client fails to initialise with the error

'An internal error occurred: ', { [Error: Client did not initialize correctly]
  type: 8,
  innerError: [TypeError: adjustConfig.setAttributionCallbackListener is not a function (it is undefined)] }

kathaypacific avatar Dec 17 '24 14:12 kathaypacific

We can write the plugin using v5 and pass that to segment. This helped to resolve the initialization error

import { Adjust, AdjustConfig } from 'react-native-adjust';

import {
  DestinationPlugin,
  IdentifyEventType,
  PluginType,
  SegmentAPISettings,
  SegmentAdjustSettings,
  TrackEventType,
  UpdateType,
} from '@segment/analytics-react-native';

import reset from './methods/reset';

import identify from './methods/identify';
import track from './methods/track';

export class AdjustPlugin extends DestinationPlugin {
  type = PluginType.destination;
  key = 'Adjust';

  private settings: SegmentAdjustSettings | null = null;
  private hasRegisteredCallback = false;

  update(settings: SegmentAPISettings, _: UpdateType) {
    const adjustSettings = settings.integrations[this.key] as SegmentAdjustSettings;

    if (adjustSettings === undefined || adjustSettings === null) {
      return;
    }

    this.settings = adjustSettings;

    const environment = this.settings.setEnvironmentProduction === true ? 'production' : 'sandbox';

    const adjustConfig = new AdjustConfig(this.settings.appToken, environment);

    if (this.hasRegisteredCallback === false) {
      adjustConfig.setAttributionCallback(attribution => {
        const trackPayload = {
          provider: 'Adjust',
          trackerToken: attribution.trackerToken,
          trackerName: attribution.trackerName,
          campaign: {
            source: attribution.network,
            name: attribution.campaign,
            content: attribution.clickLabel,
            adCreative: attribution.creative,
            adGroup: attribution.adgroup,
          },
        };
        void this.analytics?.track('Install Attributed', trackPayload);
      });
      this.hasRegisteredCallback = true;
    }

    // not supported in v5
    // const bufferingEnabled = this.settings.setEventBufferingEnabled;
    // if (bufferingEnabled === true) {
    //   adjustConfig.setEventBufferingEnabled(bufferingEnabled);
    // }

    // not supported in v5
    // const useDelay = this.settings.setDelay;
    // if (useDelay === true) {
    //   const delayTime = this.settings.delayTime;
    //   if (delayTime !== null && delayTime !== undefined) {
    //     adjustConfig.setDelayStart(delayTime);
    //   }
    // }

    Adjust.initSdk(adjustConfig);
  }
  identify(event: IdentifyEventType) {
    identify(event);
    return event;
  }

  track(event: TrackEventType) {
    track(event, this.settings!);
    return event;
  }

  reset() {
    reset();
  }
}

identify

import { Adjust } from 'react-native-adjust';

import type { IdentifyEventType } from '@segment/analytics-react-native';

export default (event: IdentifyEventType) => {
  const userId = event.userId;
  if (userId !== undefined && userId !== null && userId.length > 0) {
    Adjust.addGlobalPartnerParameter('user_id', userId);
  }

  const anonId = event.anonymousId;
  if (anonId !== undefined && anonId !== null && anonId.length > 0) {
    Adjust.addGlobalPartnerParameter('anonymous_id', anonId);
  }
};


reset

import { Adjust } from 'react-native-adjust';

export default () => {
  Adjust.removeGlobalPartnerParameters();
};

track

import { Adjust, AdjustEvent } from 'react-native-adjust';

import type { SegmentAdjustSettings, TrackEventType } from '@segment/analytics-react-native';

import { extract, mappedCustomEventToken } from './util';

export default (event: TrackEventType, settings: SegmentAdjustSettings) => {
  const anonId = event.anonymousId;
  if (anonId !== undefined && anonId !== null && anonId.length > 0) {
    Adjust.addGlobalPartnerParameter('anonymous_id', anonId);
  }

  const token = mappedCustomEventToken(event.event, settings);
  if (token !== undefined && token !== null) {
    const adjEvent = new AdjustEvent(token);

    const properties = event.properties;
    if (properties !== undefined && properties !== null) {
      Object.entries(properties).forEach(([key, value]) => {
        adjEvent.addCallbackParameter(key, value as string);
      });

      const revenue = extract<number>('revenue', properties);
      const currency = extract<string>('currency', properties, 'USD');
      const orderId = extract<string>('orderId', properties);

      if (
        revenue !== undefined &&
        revenue !== null &&
        currency !== undefined &&
        currency !== null
      ) {
        adjEvent.setRevenue(revenue, currency);
      }

      if (orderId !== undefined && orderId !== null) {
        adjEvent.setTransactionId(orderId);
      }
    }

    Adjust.trackEvent(adjEvent);
  }
};

kasunprabath98 avatar Jan 16 '25 12:01 kasunprabath98

@kathaypacific : analytics-react-native-plugin-adjust is now compatible with react-native-adjust v5. Please upgrade to latest version

sunitaprajapati89 avatar Apr 11 '25 05:04 sunitaprajapati89