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

feat(extension): posthog lace version lw-8778 #665

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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 @@ -29,13 +29,13 @@ const mockBackgroundServiceUtils = {
};

const getPostHogClient = (view = ExtensionViews.Extended) =>
new PostHogClient(
Wallet.Cardano.ChainIds.Preprod,
new PostHogClient({
chain: Wallet.Cardano.ChainIds.Preprod,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
userIdServiceMock as any,
mockBackgroundServiceUtils,
userIdService: userIdServiceMock as any,
backgroundServiceUtils: mockBackgroundServiceUtils,
view
);
});

describe('AnalyticsTracker', () => {
const preprodChain = Wallet.Cardano.ChainIds.Preprod;
Expand All @@ -50,12 +50,12 @@ describe('AnalyticsTracker', () => {
new AnalyticsTracker({ chain: preprodChain, postHogClient: getPostHogClient() });
expect(getUserIdService).toHaveBeenCalledTimes(1);
expect(MatomoClient).toHaveBeenCalledWith(preprodChain, userIdServiceMock);
expect(PostHogClient).toHaveBeenCalledWith(
preprodChain,
userIdServiceMock,
mockBackgroundServiceUtils,
ExtensionViews.Extended
);
expect(PostHogClient).toHaveBeenCalledWith({
chain: preprodChain,
userIdService: userIdServiceMock,
backgroundServiceUtils: mockBackgroundServiceUtils,
view: ExtensionViews.Extended
});
});
it('should only setup matomo client if posthog is disabled', () => {
// eslint-disable-next-line no-new
Expand All @@ -78,12 +78,12 @@ describe('AnalyticsTracker', () => {
view: ExtensionViews.Popup,
postHogClient: getPostHogClient(ExtensionViews.Popup)
});
expect(PostHogClient).toHaveBeenCalledWith(
preprodChain,
userIdServiceMock,
mockBackgroundServiceUtils,
ExtensionViews.Popup
);
expect(PostHogClient).toHaveBeenCalledWith({
chain: preprodChain,
userIdService: userIdServiceMock,
backgroundServiceUtils: mockBackgroundServiceUtils,
view: ExtensionViews.Popup
});
});
it('should setup Post Hog client with view = extended', () => {
// eslint-disable-next-line no-new
Expand All @@ -92,12 +92,12 @@ describe('AnalyticsTracker', () => {
view: ExtensionViews.Extended,
postHogClient: getPostHogClient()
});
expect(PostHogClient).toHaveBeenCalledWith(
preprodChain,
userIdServiceMock,
mockBackgroundServiceUtils,
ExtensionViews.Extended
);
expect(PostHogClient).toHaveBeenCalledWith({
chain: preprodChain,
userIdService: userIdServiceMock,
backgroundServiceUtils: mockBackgroundServiceUtils,
view: ExtensionViews.Extended
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export type PostHogMetadata = {
view: ExtensionViews;
sent_at_local: string;
posthog_project_id: number;
lace_version?: string;
} & PostHogPersonProperties;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const mockUserTrackingType$ = new BehaviorSubject<UserTrackingType>(UserTracking
jest.mock('posthog-js');

describe('PostHogClient', () => {
const publicPosthogHost = 'test';
const publicPostHogHost = 'test';
const chain = Wallet.Cardano.ChainIds.Preprod;
const userId = 'userId';
const mockUserIdService: UserIdService = {
Expand All @@ -31,20 +31,30 @@ describe('PostHogClient', () => {

it('should initialize posthog on construction', async () => {
// eslint-disable-next-line no-new
const client = new PostHogClient(chain, mockUserIdService, mockBackgroundStorageUtil, undefined, publicPosthogHost);
const client = new PostHogClient({
chain,
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
publicPostHogHost
});

await waitFor(() => expect(client).toBeDefined());
expect(posthog.init).toHaveBeenCalledWith(
expect.stringContaining(DEV_NETWORK_ID_TO_POSTHOG_TOKEN_MAP[chain.networkMagic]),
expect.objectContaining({
// eslint-disable-next-line camelcase
api_host: publicPosthogHost
api_host: publicPostHogHost
})
);
});

it('should send page navigation events with distinct id and view = extended as default', async () => {
const client = new PostHogClient(chain, mockUserIdService, mockBackgroundStorageUtil, undefined, publicPosthogHost);
const client = new PostHogClient({
chain,
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
publicPostHogHost
});
await client.sendPageNavigationEvent();
expect(posthog.capture).toHaveBeenCalledWith(
'$pageview',
Expand All @@ -57,7 +67,12 @@ describe('PostHogClient', () => {
});

it('should send events with distinct id', async () => {
const client = new PostHogClient(chain, mockUserIdService, mockBackgroundStorageUtil, undefined, publicPosthogHost);
const client = new PostHogClient({
chain,
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
publicPostHogHost
});
const event = PostHogAction.OnboardingCreateClick;
const extraProps = { some: 'prop', another: 'test' };

Expand All @@ -75,7 +90,12 @@ describe('PostHogClient', () => {

it('should be possible to change the chain', () => {
const previewChain = Wallet.Cardano.ChainIds.Preview;
const client = new PostHogClient(chain, mockUserIdService, mockBackgroundStorageUtil, undefined, publicPosthogHost);
const client = new PostHogClient({
chain,
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
publicPostHogHost
});
expect(posthog.set_config).not.toHaveBeenCalled();
client.setChain(previewChain);
expect(posthog.set_config).toHaveBeenCalledWith(
Expand All @@ -86,13 +106,13 @@ describe('PostHogClient', () => {
});

it('should send events with property view = popup', async () => {
const client = new PostHogClient(
const client = new PostHogClient({
chain,
mockUserIdService,
mockBackgroundStorageUtil,
ExtensionViews.Popup,
publicPosthogHost
);
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Popup,
publicPostHogHost
});
const event = PostHogAction.OnboardingCreateClick;

await client.sendEvent(event);
Expand All @@ -106,13 +126,13 @@ describe('PostHogClient', () => {
});

it('should send events with property view = extended', async () => {
const client = new PostHogClient(
const client = new PostHogClient({
chain,
mockUserIdService,
mockBackgroundStorageUtil,
ExtensionViews.Extended,
publicPosthogHost
);
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost
});
const event = PostHogAction.OnboardingCreateClick;

await client.sendEvent(event);
Expand All @@ -125,15 +145,37 @@ describe('PostHogClient', () => {
);
});

it('should send events with property lace_version = 1.2.3', async () => {
const client = new PostHogClient({
chain,
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost,
laceVersion: '1.2.3'
});
const event = PostHogAction.OnboardingCreateClick;

await client.sendEvent(event);

expect(posthog.capture).toHaveBeenCalledWith(
event,
expect.objectContaining({
// eslint-disable-next-line camelcase
lace_version: '1.2.3'
})
);
});

it('should send events with property sent at local', async () => {
jest.useFakeTimers().setSystemTime(mockSentDate);
const client = new PostHogClient(
const client = new PostHogClient({
chain,
mockUserIdService,
mockBackgroundStorageUtil,
ExtensionViews.Extended,
publicPosthogHost
);
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost
});
const event = PostHogAction.OnboardingCreateClick;

await client.sendEvent(event);
Expand All @@ -150,39 +192,39 @@ describe('PostHogClient', () => {
it('should send alias event if alias and id properties are defined', async () => {
const mockAliasProperties = { id: 'walletBasedId', alias: 'aliasId' };
const mockGetAliasProperties = jest.fn().mockReturnValue(mockAliasProperties);
const client = new PostHogClient(
const client = new PostHogClient({
chain,
{ ...mockUserIdService, getAliasProperties: mockGetAliasProperties },
mockBackgroundStorageUtil,
ExtensionViews.Extended,
publicPosthogHost
);
userIdService: { ...mockUserIdService, getAliasProperties: mockGetAliasProperties },
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost
});
await client.sendAliasEvent();
expect(posthog.alias).toHaveBeenCalledWith(mockAliasProperties.alias, mockAliasProperties.id);
});

it('should not send alias event if alias or id properties are not defined', async () => {
const mockGetAliasProperties = jest.fn().mockReturnValue({});
const client = new PostHogClient(
const client = new PostHogClient({
chain,
{ ...mockUserIdService, getAliasProperties: mockGetAliasProperties },
mockBackgroundStorageUtil,
ExtensionViews.Extended,
publicPosthogHost
);
userIdService: { ...mockUserIdService, getAliasProperties: mockGetAliasProperties },
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost
});
await client.sendAliasEvent();
expect(posthog.alias).not.toHaveBeenCalled();
});

it('should return user_tracking_type enhanced', async () => {
const event = PostHogAction.OnboardingCreateClick;
const client = new PostHogClient(
const client = new PostHogClient({
chain,
mockUserIdService,
mockBackgroundStorageUtil,
ExtensionViews.Extended,
publicPosthogHost
);
userIdService: mockUserIdService,
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost
});
mockUserIdService.userTrackingType$.next(UserTrackingType.Enhanced);
await client.sendEvent(event);
expect(posthog.capture).toHaveBeenCalledWith(
Expand All @@ -200,13 +242,13 @@ describe('PostHogClient', () => {
it('should return user_tracking_type basic after calling twice', async () => {
const event = PostHogAction.OnboardingCreateClick;
const tracking = new BehaviorSubject(UserTrackingType.Enhanced);
const client = new PostHogClient(
const client = new PostHogClient({
chain,
{ ...mockUserIdService, userTrackingType$: tracking },
mockBackgroundStorageUtil,
ExtensionViews.Extended,
publicPosthogHost
);
userIdService: { ...mockUserIdService, userTrackingType$: tracking },
backgroundServiceUtils: mockBackgroundStorageUtil,
view: ExtensionViews.Extended,
publicPostHogHost
});

await client.sendEvent(event);
expect(posthog.capture).toHaveBeenCalledWith(
Expand Down
Loading