Skip to content

Commit

Permalink
Include branch ref params in metrics (#4595)
Browse files Browse the repository at this point in the history
  • Loading branch information
salman-rb authored and Ibrahim Taveras committed Feb 7, 2023
1 parent 61dc219 commit 310ddc8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import { migrate } from '@/migrations';
import { initListeners as initWalletConnectListeners } from '@/utils/walletConnect';
import { getExperimetalFlag, WC_V2 } from '@/config/experimental';
import { saveFCMToken } from '@/notifications/tokens';
import branch from 'react-native-branch';

const FedoraToastRef = createRef();

Expand Down Expand Up @@ -129,7 +130,7 @@ class OldApp extends Component {
AppState.addEventListener('change', this.handleAppStateChange);
rainbowTokenList.on('update', this.handleTokenListUpdate);
appEvents.on('transactionConfirmed', this.handleTransactionConfirmed);
this.branchListener = branchListener(this.handleOpenLinkingURL);
this.branchListener = await branchListener(this.handleOpenLinkingURL);
// Walletconnect uses direct deeplinks
if (android) {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/analytics/userProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface UserProperties {
screenWidth?: number;
screenScale?: number;

// branch
branchCampaign?: string;
branchReferrer?: string;
branchReferringLink?: string;

// to be deprecated
assets_value?: number;
borrowed_value?: number;
Expand Down
6 changes: 6 additions & 0 deletions src/storage/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ export type Device = {
* already opened the app at least once.
*/
isReturningUser: boolean;

/**
* Undefined when branch referring params have not been attempted to be set in
* the past. We set this to `true` immediately after checking.
*/
branchFirstReferringParamsSet: boolean;
};
40 changes: 36 additions & 4 deletions src/utils/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import pako from 'pako';
import qs from 'qs';
import branch from 'react-native-branch';
import { IS_TESTING } from 'react-native-dotenv';
import logger from '@/utils/logger';
import { analyticsV2 } from '@/analytics';
import * as ls from '@/storage';
import { logger, RainbowError } from '@/logger';

export const branchListener = (handleOpenLinkingURL: (url: any) => void) =>
branch.subscribe(({ error, params, uri }) => {
export const branchListener = async (
handleOpenLinkingURL: (url: any) => void
) => {
const unsubscribe = branch.subscribe(({ error, params, uri }) => {
if (error) {
logger.error('Error from Branch: ' + error);
logger.error(new RainbowError('Error from Branch'), { error });
}

if (!params && uri) {
Expand Down Expand Up @@ -56,6 +60,34 @@ export const branchListener = (handleOpenLinkingURL: (url: any) => void) =>
}
});

// getFirstReferringParams must be called after branch.subscribe()
const branchFirstReferringParamsSet = ls.device.get([
'branchFirstReferringParamsSet',
]);
if (!branchFirstReferringParamsSet) {
const branchParams = await branch
.getFirstReferringParams()
.then(branchParams => branchParams)
.catch(e => {
logger.error(
new RainbowError('error calling branch.getFirstReferringParams()'),
e
);
return null;
});
if (branchParams) {
analyticsV2.identify({
branchCampaign: branchParams['~campaign'],
branchReferrer: branchParams['+referrer'],
branchReferringLink: branchParams['~referring_link'],
});
}
ls.device.set(['branchFirstReferringParamsSet'], true);
}

return unsubscribe;
};

/**
* Sometimes branch deeplinks have the following form:
* rainbow://open?_branch_referrer=a&link_click_id=b
Expand Down

0 comments on commit 310ddc8

Please sign in to comment.