Skip to content

fix: update braze logPurchase functionality #812

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

Merged
merged 4 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
DEAD_CODE_STRIPPING = NO;
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = arm64;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
INFOPLIST_FILE = AnalyticsReactNativeExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
Expand Down Expand Up @@ -321,7 +321,7 @@
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = "";
ENABLE_BITCODE = NO;
"EXCLUDED_ARCHS[sdk=*]" = arm64;
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
INFOPLIST_FILE = AnalyticsReactNativeExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LIBRARY_SEARCH_PATHS = (
Expand Down
3 changes: 1 addition & 2 deletions example/ios/AnalyticsReactNativeExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// Braze *braze = [BrazeReactBridge initBraze:configuration];
// AppDelegate.braze = braze;


return YES;
}

Expand All @@ -96,7 +95,7 @@ - (BOOL)application:(UIApplication *)application
openURL: (NSURL *)url
options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {

[AnalyticsReactNative trackDeepLink:url withOptions:options];
// [AnalyticsReactNative trackDeepLink:url withOptions:options];
return YES;
}
/// This method controls whether the `concurrentRoot`feature of React18 is turned on or off.
Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { AmplitudeSessionPlugin } from '@segment/analytics-react-native-plugin-a
// import { BrazePlugin } from '@segment/analytics-react-native-plugin-braze';

const segmentClient = createClient({
writeKey: 'WRITE_KEY',
writeKey: '<WRITE_KEY>',
trackAppLifecycleEvents: true,
collectDeviceId: true,
debug: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ export type SegmentAdjustSettings = {
delayTime?: number;
};

export type SegmentBrazeSettings = {
logPurchaseWhenRevenuePresent: boolean;
};

export type IntegrationSettings =
// Strongly typed known integration settings
| SegmentAPIIntegration
Expand Down
182 changes: 180 additions & 2 deletions packages/plugins/plugin-braze/src/BrazePlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,43 @@ import {
UserInfoState,
isObject,
objectToString,
SegmentAPISettings,
UpdateType,
JsonMap,
SegmentBrazeSettings,
} from '@segment/analytics-react-native';
import Braze, { GenderTypes, MonthsAsNumber } from '@braze/react-native-sdk';
import flush from './methods/flush';
import track from './methods/track';
import { unknownToString } from '../../../core/src/util';

interface AttributionProperties {
network: string;
campaign: string;
adGroup: string;
creative: string;
}

const defaultProperties: AttributionProperties = {
network: '',
campaign: '',
adGroup: '',
creative: '',
};

export class BrazePlugin extends DestinationPlugin {
type = PluginType.destination;
key = 'Appboy';
private lastSeenTraits: UserInfoState | undefined;
private revenueEnabled = false;

update(settings: SegmentAPISettings, _: UpdateType) {
const brazeSettings = settings.integrations[
this.key
] as SegmentBrazeSettings;
if (brazeSettings.logPurchaseWhenRevenuePresent === true) {
this.revenueEnabled = true;
}
}

/**
* Cleans up the attributes to only send valid values to Braze SDK
Expand Down Expand Up @@ -153,11 +181,161 @@ export class BrazePlugin extends DestinationPlugin {
}

track(event: TrackEventType) {
track(event);
const eventName = event.event;
const revenue = this.extractRevenue(event.properties, 'revenue');

if (event.event === 'Install Attributed') {
if (
event.properties?.campaign !== undefined &&
event.properties?.campaign !== null
) {
const attributionData: unknown = event.properties.campaign;
let network: string,
campaign: string,
adGroup: string,
creative: string;

if (isObject(attributionData)) {
network =
unknownToString(
attributionData.source,
true,
undefined,
undefined
) ?? defaultProperties.network;
campaign =
unknownToString(attributionData.name, true, undefined, undefined) ??
defaultProperties.campaign;
adGroup =
unknownToString(
attributionData.ad_group,
true,
undefined,
undefined
) ?? defaultProperties.adGroup;
creative =
unknownToString(
attributionData.ad_creative,
true,
undefined,
undefined
) ?? defaultProperties.creative;
} else {
network = defaultProperties.network;
campaign = defaultProperties.campaign;
adGroup = defaultProperties.adGroup;
creative = defaultProperties.creative;
}
Braze.setAttributionData(network, campaign, adGroup, creative);
}
}

if (eventName === 'Order Completed' || eventName === 'Completed Order') {
this.logPurchaseEvent(event);
} else if (
this.revenueEnabled === true &&
revenue !== 0 &&
revenue !== undefined
) {
this.logPurchaseEvent(event);
} else {
Braze.logCustomEvent(eventName, event.properties);
}
return event;
}

flush() {
flush();
}

extractRevenue = (properties: JsonMap | undefined, key: string) => {
if (!properties) {
return 0;
}

const revenue = properties[key];
if (revenue !== undefined && revenue !== null) {
switch (typeof revenue) {
case 'string':
return parseFloat(revenue);
case 'number':
return revenue;
default:
return 0;
}
} else {
return 0;
}
};

logPurchaseEvent(event: TrackEventType) {
// Make USD as the default currency.
let currency = 'USD';
const revenue = this.extractRevenue(event.properties, 'revenue');
if (
typeof event.properties?.currency === 'string' &&
event.properties.currency.length === 3
) {
currency = event.properties.currency;
}
if (event.properties) {
const appBoyProperties = Object.assign({}, event.properties);
delete appBoyProperties.currency;
delete appBoyProperties.revenue;

if (
appBoyProperties.products !== undefined &&
appBoyProperties.products !== null
) {
const products = (appBoyProperties.products as unknown[]).slice(0);
delete appBoyProperties.products;

products.forEach((product) => {
const productDict = Object.assign(
{},
isObject(product) ? product : {}
);
const productId =
unknownToString(
productDict.product_id,
true,
undefined,
undefined
) ?? '';
const productRevenue = this.extractRevenue(
productDict as unknown as JsonMap,
'price'
);
const productQuantity = isNumber(productDict.quantity)
? productDict.quantity
: 1;
delete productDict.product_id;
delete productDict.price;
delete productDict.quantity;
const productProperties = Object.assign(
{},
appBoyProperties,
productDict
);
Braze.logPurchase(
unknownToString(productId) ?? '',
String(productRevenue),
currency,
productQuantity,
productProperties
);
});
} else {
Braze.logPurchase(
event.event,
String(revenue),
currency,
1,
appBoyProperties
);
}
} else {
Braze.logPurchase(event.event, String(revenue), currency, 1);
}
}
}
Loading