Skip to content

Commit

Permalink
feat(llm): add a braze plugin to segment to debounce the identify tra…
Browse files Browse the repository at this point in the history
…its and reduce the data points usage
  • Loading branch information
cgrellard-ledger committed Apr 19, 2024
1 parent 19578f0 commit 81706ce
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-ligers-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": minor
---

LLM - Add a Braze Plugin to segment to debounce the identify traits and reduce the data points usage
44 changes: 44 additions & 0 deletions apps/ledger-live-mobile/src/analytics/BrazePlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
Plugin,
PluginType,
UserInfoState,
SegmentEvent,
EventType,
} from "@segment/analytics-react-native";
import isEqual from "lodash/isEqual";

export class BrazePlugin extends Plugin {
type = PluginType.before;
key = "Appboy";
private lastSeenTraits: UserInfoState | undefined = undefined;

execute(event: SegmentEvent): SegmentEvent | undefined {
if (event.type === EventType.IdentifyEvent) {
// We don't check for some traits as they are sure to be different every time
const traits = {
...event.traits,
appTimeToInteractiveMilliseconds: undefined,
stakingProvidersEnabled: undefined,
};
if (
this.lastSeenTraits?.userId === event.userId &&
this.lastSeenTraits?.anonymousId === event.anonymousId &&
isEqual(this.lastSeenTraits?.traits, traits)
) {
const integrations = event.integrations;

// If the traits didn't changed, disable braze integration
if (integrations !== undefined) {
integrations[this.key] = false;
}
} else {
this.lastSeenTraits = {
anonymousId: event.anonymousId ?? "",
userId: event.userId,
traits: traits,
};
}
}
return event;
}
}
3 changes: 3 additions & 0 deletions apps/ledger-live-mobile/src/analytics/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { NavigatorName } from "~/const";
import { previousRouteNameRef, currentRouteNameRef } from "./screenRefs";
import { AnonymousIpPlugin } from "./AnonymousIpPlugin";
import { UserIdPlugin } from "./UserIdPlugin";
import { BrazePlugin } from "./BrazePlugin";
import { Maybe } from "../types/helpers";
import { appStartupTime } from "../StartupTimeMarker";
import { aggregateData, getUniqueModelIdList } from "../logic/modelIdList";
Expand Down Expand Up @@ -227,6 +228,8 @@ export const start = async (store: AppStore): Promise<SegmentClient | undefined>
segmentClient.add({ plugin: new AnonymousIpPlugin() });
// This allows us to make sure we are adding the userId to the event
segmentClient.add({ plugin: new UserIdPlugin() });
// This allows us to debounce identify events for Braze and save data points
segmentClient.add({ plugin: new BrazePlugin() });

if (created) {
segmentClient.reset();
Expand Down

0 comments on commit 81706ce

Please sign in to comment.