Skip to content

Commit 5d9c106

Browse files
committed
feat: implement userpilot analytics plugin and remove segment plugin
1 parent a5b54b3 commit 5d9c106

File tree

4 files changed

+68
-28
lines changed

4 files changed

+68
-28
lines changed

src/@types/analytics__segment/index.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/analytics.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Analytics from 'analytics';
21
import googleTagManager from '@analytics/google-tag-manager';
3-
import segmentPlugin from '@analytics/segment';
2+
import Analytics from 'analytics';
3+
import userpilot from './common/analytics-plugins/userpilot';
44
import { isDev } from './common/isDevEnvironment';
55

66
const analytics = Analytics({
@@ -13,10 +13,8 @@ const analytics = Analytics({
1313
preview: 'env-4',
1414
}),
1515
}),
16-
segmentPlugin({
17-
writeKey: isDev()
18-
? 'AxHbacd31w50NwjDM8tadsP82hSwTz4Z'
19-
: 'oxd3W7coKxDdzq99F88doV5VvQrESbJh',
16+
userpilot({
17+
token: isDev() ? 'STG-NX-54e88e10' : 'NX-54e88e10',
2018
}),
2119
],
2220
});

src/common/Track.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const Track = ({
5353
name: userData.name,
5454
email: userData.email,
5555
company: activeWorkspace.company,
56+
workspace: activeWorkspace,
5657
});
5758

5859
track(
@@ -68,8 +69,8 @@ export const Track = ({
6869
},
6970
{
7071
plugins: {
71-
// disable track event for segment
72-
segment: false,
72+
// disable userpilot for this event
73+
userpilot: false,
7374
},
7475
}
7576
);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { User } from 'src/features/api';
2+
import { Userpilot } from 'userpilot';
3+
4+
declare global {
5+
interface Window {
6+
userpilot?: {
7+
initialized: number;
8+
};
9+
}
10+
}
11+
12+
interface UserpilotConfig {
13+
token: string;
14+
name?: string;
15+
}
16+
17+
interface IPayload {
18+
type: string;
19+
userId: string;
20+
}
21+
22+
interface IIdentifyPayload extends IPayload {
23+
traits: User & { workspace: { id: number; company: string } };
24+
}
25+
26+
export default function userpilotPlugin(pluginSettings: UserpilotConfig) {
27+
return {
28+
name: 'userpilot',
29+
config: { ...pluginSettings },
30+
initialize: ({ config }: { config: UserpilotConfig }) => {
31+
Userpilot.initialize(config.token);
32+
},
33+
identify: ({ payload }: { payload: IIdentifyPayload }) => {
34+
const { userId, traits } = payload;
35+
const { workspace, ...basicTraits } = traits;
36+
37+
Userpilot.identify(userId, {
38+
...basicTraits,
39+
company: {
40+
id: workspace.id,
41+
name: workspace.company,
42+
},
43+
});
44+
},
45+
46+
page: () => {
47+
Userpilot.reload();
48+
},
49+
50+
track: ({
51+
payload,
52+
}: {
53+
payload: IPayload & { event: string; properties: Record<string, any> };
54+
}) => {
55+
const { event, properties, userId } = payload;
56+
Userpilot.track(event, { ...properties, userId });
57+
},
58+
59+
loaded: () => !!window.userpilot,
60+
};
61+
}

0 commit comments

Comments
 (0)