Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kathaypacific opened this issue Dec 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kathaypacific
Copy link

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 kathaypacific added the bug Something isn't working label Dec 17, 2024
@kasunprabath98
Copy link

kasunprabath98 commented Jan 16, 2025

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);
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants