@posthog/wizard 1.6.2 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/__tests__/run.test.d.ts +1 -0
- package/dist/src/__tests__/run.test.js +48 -0
- package/dist/src/__tests__/run.test.js.map +1 -0
- package/dist/src/run.js +5 -0
- package/dist/src/run.js.map +1 -1
- package/dist/src/utils/__tests__/analytics.test.d.ts +1 -0
- package/dist/src/utils/__tests__/analytics.test.js +126 -0
- package/dist/src/utils/__tests__/analytics.test.js.map +1 -0
- package/dist/src/utils/analytics.d.ts +1 -0
- package/dist/src/utils/analytics.js +8 -0
- package/dist/src/utils/analytics.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const run_1 = require("../run");
|
|
4
|
+
const nextjs_wizard_1 = require("../nextjs/nextjs-wizard");
|
|
5
|
+
const analytics_1 = require("../utils/analytics");
|
|
6
|
+
const constants_1 = require("../lib/constants");
|
|
7
|
+
jest.mock('../nextjs/nextjs-wizard');
|
|
8
|
+
jest.mock('../utils/analytics');
|
|
9
|
+
jest.mock('../utils/clack');
|
|
10
|
+
const mockRunNextjsWizard = nextjs_wizard_1.runNextjsWizard;
|
|
11
|
+
const mockAnalytics = analytics_1.analytics;
|
|
12
|
+
describe('runWizard error handling', () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
jest.clearAllMocks();
|
|
15
|
+
mockAnalytics.setTag = jest.fn();
|
|
16
|
+
mockAnalytics.captureException = jest.fn();
|
|
17
|
+
mockAnalytics.shutdown = jest.fn().mockResolvedValue(undefined);
|
|
18
|
+
jest.spyOn(process, 'exit').mockImplementation(() => {
|
|
19
|
+
throw new Error('process.exit called');
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
jest.restoreAllMocks();
|
|
24
|
+
});
|
|
25
|
+
it('should capture exception and shutdown analytics on wizard error', async () => {
|
|
26
|
+
const testError = new Error('Wizard failed');
|
|
27
|
+
const testArgs = {
|
|
28
|
+
integration: constants_1.Integration.nextjs,
|
|
29
|
+
debug: true,
|
|
30
|
+
forceInstall: false,
|
|
31
|
+
};
|
|
32
|
+
mockRunNextjsWizard.mockRejectedValue(testError);
|
|
33
|
+
await expect((0, run_1.runWizard)(testArgs)).rejects.toThrow('process.exit called');
|
|
34
|
+
expect(mockAnalytics.captureException).toHaveBeenCalledWith(testError, {
|
|
35
|
+
integration: constants_1.Integration.nextjs,
|
|
36
|
+
arguments: JSON.stringify(testArgs),
|
|
37
|
+
});
|
|
38
|
+
expect(mockAnalytics.shutdown).toHaveBeenCalledWith('error');
|
|
39
|
+
});
|
|
40
|
+
it('should not call captureException when wizard succeeds', async () => {
|
|
41
|
+
const testArgs = { integration: constants_1.Integration.nextjs };
|
|
42
|
+
mockRunNextjsWizard.mockResolvedValue(undefined);
|
|
43
|
+
await (0, run_1.runWizard)(testArgs);
|
|
44
|
+
expect(mockAnalytics.captureException).not.toHaveBeenCalled();
|
|
45
|
+
expect(mockAnalytics.shutdown).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
//# sourceMappingURL=run.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.test.js","sourceRoot":"","sources":["../../../src/__tests__/run.test.ts"],"names":[],"mappings":";;AAAA,gCAAmC;AACnC,2DAA0D;AAC1D,kDAA+C;AAC/C,gDAA+C;AAE/C,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;AACrC,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;AAChC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;AAE5B,MAAM,mBAAmB,GAAG,+BAE3B,CAAC;AACF,MAAM,aAAa,GA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AAa,CAAC,QAAQ,CAAC,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,QAAQ,GAAG,EAAE,WAAW,EAAE,uBAAW,CAAC,MAAM,EAAE,CAAC;QAErD,mBAAmB,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC;QAEjD,MAAM,IAAA,eAAS,EAAC,QAAQ,CAAC,CAAC;QAE1B,MAAM,CAAC,aAAa,CAAC,gBAAgB,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;QAC9D,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC;IACxD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { runWizard } from '../run';\nimport { runNextjsWizard } from '../nextjs/nextjs-wizard';\nimport { analytics } from '../utils/analytics';\nimport { Integration } from '../lib/constants';\n\njest.mock('../nextjs/nextjs-wizard');\njest.mock('../utils/analytics');\njest.mock('../utils/clack');\n\nconst mockRunNextjsWizard = runNextjsWizard as jest.MockedFunction<\n typeof runNextjsWizard\n>;\nconst mockAnalytics = analytics as jest.Mocked<typeof analytics>;\n\ndescribe('runWizard error handling', () => {\n beforeEach(() => {\n jest.clearAllMocks();\n\n mockAnalytics.setTag = jest.fn();\n mockAnalytics.captureException = jest.fn();\n mockAnalytics.shutdown = jest.fn().mockResolvedValue(undefined);\n\n jest.spyOn(process, 'exit').mockImplementation(() => {\n throw new Error('process.exit called');\n });\n });\n\n afterEach(() => {\n jest.restoreAllMocks();\n });\n\n it('should capture exception and shutdown analytics on wizard error', async () => {\n const testError = new Error('Wizard failed');\n const testArgs = {\n integration: Integration.nextjs,\n debug: true,\n forceInstall: false,\n };\n\n mockRunNextjsWizard.mockRejectedValue(testError);\n\n await expect(runWizard(testArgs)).rejects.toThrow('process.exit called');\n\n expect(mockAnalytics.captureException).toHaveBeenCalledWith(testError, {\n integration: Integration.nextjs,\n arguments: JSON.stringify(testArgs),\n });\n\n expect(mockAnalytics.shutdown).toHaveBeenCalledWith('error');\n });\n\n it('should not call captureException when wizard succeeds', async () => {\n const testArgs = { integration: Integration.nextjs };\n\n mockRunNextjsWizard.mockResolvedValue(undefined);\n\n await runWizard(testArgs);\n\n expect(mockAnalytics.captureException).not.toHaveBeenCalled();\n expect(mockAnalytics.shutdown).not.toHaveBeenCalled();\n });\n});\n"]}
|
package/dist/src/run.js
CHANGED
|
@@ -69,6 +69,11 @@ async function runWizard(argv) {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
catch (error) {
|
|
72
|
+
analytics_1.analytics.captureException(error, {
|
|
73
|
+
integration,
|
|
74
|
+
arguments: JSON.stringify(finalArgs),
|
|
75
|
+
});
|
|
76
|
+
await analytics_1.analytics.shutdown('error');
|
|
72
77
|
clack_1.default.log.error(`Something went wrong. You can read the documentation for PostHog at ${chalk_1.default.cyan(`${config_1.INTEGRATION_CONFIG[integration].docsUrl}`)} to setup PostHog manually.`);
|
|
73
78
|
process.exit(1);
|
|
74
79
|
}
|
package/dist/src/run.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/run.ts"],"names":[],"mappings":";;;;;AA8BA,
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/run.ts"],"names":[],"mappings":";;;;;AA8BA,8BAmEC;AAjGD,qDAAuD;AAEvD,0DAAyD;AAGzD,+CAAyE;AACzE,qDAAsD;AACtD,0DAAkC;AAClC,gDAAwB;AACxB,yCAAqE;AACrE,uDAAsD;AACtD,iDAA8C;AAC9C,0DAAyD;AACzD,4EAA0E;AAC1E,uDAAsD;AACtD,mCAAsC;AACtC,kDAA0B;AAE1B,qBAAY,CAAC,mBAAmB,GAAG,EAAE,CAAC;AAY/B,KAAK,UAAU,SAAS,CAAC,IA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,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC9C,IAAI,QAAQ,EAAE,CAAC;YACb,OAAO,WAA0B,CAAC;QACpC,CAAC;IACH,CAAC;AACH,CAAC;AAED,KAAK,UAAU,sBAAsB,CACnC,OAA0C;IAE1C,MAAM,mBAAmB,GAAG,MAAM,iBAAiB,CAAC,OAAO,CAAC,CAAC;IAE7D,IAAI,mBAAmB,EAAE,CAAC;QACxB,eAAK,CAAC,GAAG,CAAC,OAAO,CACf,yBAAyB,IAAA,qCAAyB,EAAC,mBAAmB,CAAC,EAAE,CAC1E,CAAC;QACF,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAED,MAAM,WAAW,GAAgB,MAAM,IAAA,8BAAgB,EACrD,eAAK,CAAC,MAAM,CAAC;QACX,OAAO,EAAE,6BAA6B;QACtC,OAAO,EAAE;YACP,EAAE,KAAK,EAAE,uBAAW,CAAC,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE;YAC/C,EAAE,KAAK,EAAE,uBAAW,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE;YAC5C,EAAE,KAAK,EAAE,uBAAW,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE;YAC5C,EAAE,KAAK,EAAE,uBAAW,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE;YAC9C,EAAE,KAAK,EAAE,uBAAW,CAAC,WAAW,EAAE,KAAK,EAAE,cAAc,EAAE;SAC1D;KACF,CAAC,CACH,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import { abortIfCancelled } from './utils/clack-utils';\n\nimport { runNextjsWizard } from './nextjs/nextjs-wizard';\nimport type { CloudRegion, WizardOptions } from './utils/types';\n\nimport { getIntegrationDescription, Integration } from './lib/constants';\nimport { readEnvironment } from './utils/environment';\nimport clack from './utils/clack';\nimport path from 'path';\nimport { INTEGRATION_CONFIG, INTEGRATION_ORDER } from './lib/config';\nimport { runReactWizard } from './react/react-wizard';\nimport { analytics } from './utils/analytics';\nimport { runSvelteWizard } from './svelte/svelte-wizard';\nimport { runReactNativeWizard } from './react-native/react-native-wizard';\nimport { runAstroWizard } from './astro/astro-wizard';\nimport { EventEmitter } from 'events';\nimport chalk from 'chalk';\n\nEventEmitter.defaultMaxListeners = 50;\n\ntype Args = {\n integration?: Integration;\n debug?: boolean;\n forceInstall?: boolean;\n installDir?: string;\n region?: CloudRegion;\n default?: boolean;\n signup?: boolean;\n};\n\nexport async function runWizard(argv: Args) {\n const finalArgs = {\n ...argv,\n ...readEnvironment(),\n };\n\n let resolvedInstallDir: string;\n if (finalArgs.installDir) {\n if (path.isAbsolute(finalArgs.installDir)) {\n resolvedInstallDir = finalArgs.installDir;\n } else {\n resolvedInstallDir = path.join(process.cwd(), finalArgs.installDir);\n }\n } else {\n resolvedInstallDir = process.cwd();\n }\n\n const wizardOptions: WizardOptions = {\n debug: finalArgs.debug ?? false,\n forceInstall: finalArgs.forceInstall ?? false,\n installDir: resolvedInstallDir,\n cloudRegion: finalArgs.region ?? undefined,\n default: finalArgs.default ?? false,\n signup: finalArgs.signup ?? false,\n };\n\n clack.intro(`Welcome to the PostHog setup wizard ✨`);\n\n const integration =\n finalArgs.integration ?? (await getIntegrationForSetup(wizardOptions));\n\n analytics.setTag('integration', integration);\n\n try {\n switch (integration) {\n case Integration.nextjs:\n await runNextjsWizard(wizardOptions);\n break;\n case Integration.react:\n await runReactWizard(wizardOptions);\n break;\n case Integration.svelte:\n await runSvelteWizard(wizardOptions);\n break;\n case Integration.reactNative:\n await runReactNativeWizard(wizardOptions);\n break;\n case Integration.astro:\n await runAstroWizard(wizardOptions);\n break;\n default:\n clack.log.error('No setup wizard selected!');\n }\n } catch (error) {\n analytics.captureException(error as Error, {\n integration,\n arguments: JSON.stringify(finalArgs),\n });\n\n await analytics.shutdown('error');\n clack.log.error(\n `Something went wrong. You can read the documentation for PostHog at ${chalk.cyan(\n `${INTEGRATION_CONFIG[integration].docsUrl}`,\n )} to setup PostHog manually.`,\n );\n process.exit(1);\n }\n}\n\nasync function detectIntegration(\n options: Pick<WizardOptions, 'installDir'>,\n): Promise<Integration | undefined> {\n const integrationConfigs = Object.entries(INTEGRATION_CONFIG).sort(\n ([a], [b]) =>\n INTEGRATION_ORDER.indexOf(a as Integration) -\n INTEGRATION_ORDER.indexOf(b as Integration),\n );\n\n for (const [integration, config] of integrationConfigs) {\n const detected = await config.detect(options);\n if (detected) {\n return integration as Integration;\n }\n }\n}\n\nasync function getIntegrationForSetup(\n options: Pick<WizardOptions, 'installDir'>,\n) {\n const detectedIntegration = await detectIntegration(options);\n\n if (detectedIntegration) {\n clack.log.success(\n `Detected integration: ${getIntegrationDescription(detectedIntegration)}`,\n );\n return detectedIntegration;\n }\n\n const integration: Integration = await abortIfCancelled(\n clack.select({\n message: 'What do you want to set up?',\n options: [\n { value: Integration.nextjs, label: 'Next.js' },\n { value: Integration.astro, label: 'Astro' },\n { value: Integration.react, label: 'React' },\n { value: Integration.svelte, label: 'Svelte' },\n { value: Integration.reactNative, label: 'React Native' },\n ],\n }),\n );\n\n return integration;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const analytics_1 = require("../analytics");
|
|
4
|
+
const posthog_node_1 = require("posthog-node");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
jest.mock('posthog-node');
|
|
7
|
+
jest.mock('uuid');
|
|
8
|
+
const mockUuidv4 = uuid_1.v4;
|
|
9
|
+
const MockedPostHog = posthog_node_1.PostHog;
|
|
10
|
+
describe('Analytics', () => {
|
|
11
|
+
let analytics;
|
|
12
|
+
let mockPostHogInstance;
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
jest.clearAllMocks();
|
|
15
|
+
mockUuidv4.mockReturnValue('test-uuid');
|
|
16
|
+
mockPostHogInstance = {
|
|
17
|
+
capture: jest.fn(),
|
|
18
|
+
captureException: jest.fn(),
|
|
19
|
+
alias: jest.fn(),
|
|
20
|
+
shutdown: jest.fn().mockResolvedValue(undefined),
|
|
21
|
+
};
|
|
22
|
+
MockedPostHog.mockImplementation(() => mockPostHogInstance);
|
|
23
|
+
analytics = new analytics_1.Analytics();
|
|
24
|
+
});
|
|
25
|
+
describe('captureException', () => {
|
|
26
|
+
it('should capture exception with error object and properties', () => {
|
|
27
|
+
const error = new Error('Test error');
|
|
28
|
+
const properties = { integration: 'nextjs' };
|
|
29
|
+
analytics.captureException(error, properties);
|
|
30
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
31
|
+
team: 'growth',
|
|
32
|
+
...properties,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
it('should capture exception with tags included in properties', () => {
|
|
36
|
+
const error = new Error('Test error');
|
|
37
|
+
const properties = { integration: 'nextjs' };
|
|
38
|
+
analytics.setTag('testTag', 'testValue');
|
|
39
|
+
analytics.captureException(error, properties);
|
|
40
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
41
|
+
team: 'growth',
|
|
42
|
+
testTag: 'testValue',
|
|
43
|
+
...properties,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it('should capture exception with distinct ID when set', () => {
|
|
47
|
+
const error = new Error('Test error');
|
|
48
|
+
const distinctId = 'user-123';
|
|
49
|
+
analytics.setDistinctId(distinctId);
|
|
50
|
+
analytics.captureException(error);
|
|
51
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, distinctId, {
|
|
52
|
+
team: 'growth',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
it('should capture exception without properties when not provided', () => {
|
|
56
|
+
const error = new Error('Test error');
|
|
57
|
+
analytics.captureException(error);
|
|
58
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
59
|
+
team: 'growth',
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it('should merge tags with provided properties', () => {
|
|
63
|
+
const error = new Error('Test error');
|
|
64
|
+
const properties = { integration: 'nextjs', step: 'installation' };
|
|
65
|
+
analytics.setTag('environment', 'test');
|
|
66
|
+
analytics.setTag('version', '1.0.0');
|
|
67
|
+
analytics.captureException(error, properties);
|
|
68
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
69
|
+
team: 'growth',
|
|
70
|
+
environment: 'test',
|
|
71
|
+
version: '1.0.0',
|
|
72
|
+
integration: 'nextjs',
|
|
73
|
+
step: 'installation',
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
it('should override tags with properties when keys conflict', () => {
|
|
77
|
+
const error = new Error('Test error');
|
|
78
|
+
const properties = { integration: 'react' };
|
|
79
|
+
analytics.setTag('integration', 'nextjs');
|
|
80
|
+
analytics.captureException(error, properties);
|
|
81
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
82
|
+
team: 'growth',
|
|
83
|
+
integration: 'react',
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
it('should always include team:growth property in exceptions', () => {
|
|
87
|
+
const error = new Error('Test error');
|
|
88
|
+
analytics.captureException(error);
|
|
89
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
90
|
+
team: 'growth',
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe('integration with other methods', () => {
|
|
95
|
+
it('should work correctly with setTag and captureException', () => {
|
|
96
|
+
const error = new Error('Test error');
|
|
97
|
+
analytics.setTag('integration', 'nextjs');
|
|
98
|
+
analytics.setTag('forceInstall', true);
|
|
99
|
+
analytics.setTag('debug', false);
|
|
100
|
+
analytics.captureException(error, {
|
|
101
|
+
arguments: JSON.stringify({ installDir: '/test' }),
|
|
102
|
+
step: 'wizard-execution',
|
|
103
|
+
});
|
|
104
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, 'test-uuid', {
|
|
105
|
+
team: 'growth',
|
|
106
|
+
integration: 'nextjs',
|
|
107
|
+
forceInstall: true,
|
|
108
|
+
debug: false,
|
|
109
|
+
arguments: JSON.stringify({ installDir: '/test' }),
|
|
110
|
+
step: 'wizard-execution',
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
it('should work correctly with setDistinctId and captureException', () => {
|
|
114
|
+
const error = new Error('Test error');
|
|
115
|
+
const distinctId = 'user-456';
|
|
116
|
+
analytics.setDistinctId(distinctId);
|
|
117
|
+
analytics.setTag('integration', 'svelte');
|
|
118
|
+
analytics.captureException(error);
|
|
119
|
+
expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(error, distinctId, {
|
|
120
|
+
team: 'growth',
|
|
121
|
+
integration: 'svelte',
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
//# sourceMappingURL=analytics.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analytics.test.js","sourceRoot":"","sources":["../../../../src/utils/__tests__/analytics.test.ts"],"names":[],"mappings":";;AAAA,4CAAyC;AACzC,+CAAuC;AACvC,+BAAoC;AAEpC,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;AAC1B,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAElB,MAAM,UAAU,GAAG,SAA4C,CAAC;AAChE,MAAM,aAAa,GAAG,sBAA2C,CAAC;AAElE,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,IAAI,SAAoB,CAAC;IACzB,IAAI,mBAAyC,CAAC;IAE9C,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,UAAU,CAAC,eAAe,CAAC,WAAkB,CAAC,CAAC;QAE/C,mBAAmB,GAAG;YACpB,OAAO,EAAE,IAA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AAC;YACvC,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAEjC,SAAS,CAAC,gBAAgB,CAAC,KAAK,EAAE;gBAChC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;gBAClD,IAAI,EAAE,kBAAkB;aACzB,CAAC,CAAC;YAEH,MAAM,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CAAC,oBAAoB,CAC/D,KAAK,EACL,WAAW,EACX;gBACE,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,QAAQ;gBACrB,YAAY,EAAE,IAAI;gBAClB,KAAK,EAAE,KAAK;gBACZ,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;gBAClD,IAAI,EAAE,kBAAkB;aACzB,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,+DAA+D,EAAE,GAAG,EAAE;YACvE,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC;YACtC,MAAM,UAAU,GAAG,UAAU,CAAC;YAE9B,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;YACpC,SAAS,CAAC,MAAM,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;YAC1C,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;YAElC,MAAM,CAAC,mBAAmB,CAAC,gBAAgB,CAAC,CAAC,oBAAoB,CAC/D,KAAK,EACL,UAAU,EACV;gBACE,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,QAAQ;aACtB,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { Analytics } from '../analytics';\nimport { PostHog } from 'posthog-node';\nimport { v4 as uuidv4 } from 'uuid';\n\njest.mock('posthog-node');\njest.mock('uuid');\n\nconst mockUuidv4 = uuidv4 as jest.MockedFunction<typeof uuidv4>;\nconst MockedPostHog = PostHog as jest.MockedClass<typeof PostHog>;\n\ndescribe('Analytics', () => {\n let analytics: Analytics;\n let mockPostHogInstance: jest.Mocked<PostHog>;\n\n beforeEach(() => {\n jest.clearAllMocks();\n mockUuidv4.mockReturnValue('test-uuid' as any);\n\n mockPostHogInstance = {\n capture: jest.fn(),\n captureException: jest.fn(),\n alias: jest.fn(),\n shutdown: jest.fn().mockResolvedValue(undefined),\n } as any;\n\n MockedPostHog.mockImplementation(() => mockPostHogInstance);\n\n analytics = new Analytics();\n });\n\n describe('captureException', () => {\n it('should capture exception with error object and properties', () => {\n const error = new Error('Test error');\n const properties = { integration: 'nextjs' };\n\n analytics.captureException(error, properties);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n ...properties,\n },\n );\n });\n\n it('should capture exception with tags included in properties', () => {\n const error = new Error('Test error');\n const properties = { integration: 'nextjs' };\n\n analytics.setTag('testTag', 'testValue');\n analytics.captureException(error, properties);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n testTag: 'testValue',\n ...properties,\n },\n );\n });\n\n it('should capture exception with distinct ID when set', () => {\n const error = new Error('Test error');\n const distinctId = 'user-123';\n\n analytics.setDistinctId(distinctId);\n analytics.captureException(error);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n distinctId,\n {\n team: 'growth',\n },\n );\n });\n\n it('should capture exception without properties when not provided', () => {\n const error = new Error('Test error');\n\n analytics.captureException(error);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n },\n );\n });\n\n it('should merge tags with provided properties', () => {\n const error = new Error('Test error');\n const properties = { integration: 'nextjs', step: 'installation' };\n\n analytics.setTag('environment', 'test');\n analytics.setTag('version', '1.0.0');\n analytics.captureException(error, properties);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n environment: 'test',\n version: '1.0.0',\n integration: 'nextjs',\n step: 'installation',\n },\n );\n });\n\n it('should override tags with properties when keys conflict', () => {\n const error = new Error('Test error');\n const properties = { integration: 'react' };\n\n analytics.setTag('integration', 'nextjs');\n analytics.captureException(error, properties);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n integration: 'react',\n },\n );\n });\n\n it('should always include team:growth property in exceptions', () => {\n const error = new Error('Test error');\n\n analytics.captureException(error);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n },\n );\n });\n });\n\n describe('integration with other methods', () => {\n it('should work correctly with setTag and captureException', () => {\n const error = new Error('Test error');\n\n analytics.setTag('integration', 'nextjs');\n analytics.setTag('forceInstall', true);\n analytics.setTag('debug', false);\n\n analytics.captureException(error, {\n arguments: JSON.stringify({ installDir: '/test' }),\n step: 'wizard-execution',\n });\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n 'test-uuid',\n {\n team: 'growth',\n integration: 'nextjs',\n forceInstall: true,\n debug: false,\n arguments: JSON.stringify({ installDir: '/test' }),\n step: 'wizard-execution',\n },\n );\n });\n\n it('should work correctly with setDistinctId and captureException', () => {\n const error = new Error('Test error');\n const distinctId = 'user-456';\n\n analytics.setDistinctId(distinctId);\n analytics.setTag('integration', 'svelte');\n analytics.captureException(error);\n\n expect(mockPostHogInstance.captureException).toHaveBeenCalledWith(\n error,\n distinctId,\n {\n team: 'growth',\n integration: 'svelte',\n },\n );\n });\n });\n});\n"]}
|
|
@@ -6,6 +6,7 @@ export declare class Analytics {
|
|
|
6
6
|
constructor();
|
|
7
7
|
setDistinctId(distinctId: string): void;
|
|
8
8
|
setTag(key: string, value: string | boolean | number | null | undefined): void;
|
|
9
|
+
captureException(error: Error, properties?: Record<string, unknown>): void;
|
|
9
10
|
capture(eventName: string, properties?: Record<string, unknown>): void;
|
|
10
11
|
shutdown(status: 'success' | 'error' | 'cancelled'): Promise<void>;
|
|
11
12
|
}
|
|
@@ -14,6 +14,7 @@ class Analytics {
|
|
|
14
14
|
host: constants_1.ANALYTICS_HOST_URL,
|
|
15
15
|
flushAt: 1,
|
|
16
16
|
flushInterval: 0,
|
|
17
|
+
enableExceptionAutocapture: true,
|
|
17
18
|
});
|
|
18
19
|
this.tags = {};
|
|
19
20
|
this.anonymousId = (0, uuid_1.v4)();
|
|
@@ -29,6 +30,13 @@ class Analytics {
|
|
|
29
30
|
setTag(key, value) {
|
|
30
31
|
this.tags[key] = value;
|
|
31
32
|
}
|
|
33
|
+
captureException(error, properties = {}) {
|
|
34
|
+
this.client.captureException(error, this.distinctId ?? this.anonymousId, {
|
|
35
|
+
team: 'growth',
|
|
36
|
+
...this.tags,
|
|
37
|
+
...properties,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
32
40
|
capture(eventName, properties) {
|
|
33
41
|
this.client.capture({
|
|
34
42
|
distinctId: this.distinctId ?? this.anonymousId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics.js","sourceRoot":"","sources":["../../../src/utils/analytics.ts"],"names":[],"mappings":";;;AAAA,+CAAuC;AACvC,gDAG0B;AAC1B,+BAAoC;AACpC,MAAa,SAAS;IACZ,MAAM,CAAU;IAChB,IAAI,GACV,EAAE,CAAC;IACG,UAAU,CAAU;IACpB,WAAW,CAAS;IAE5B;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,sBAAO,CAAC,sDAA0C,EAAE;YACpE,IAAI,EAAE,8BAAkB;YACxB,OAAO,EAAE,CAAC;YACV,aAAa,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"analytics.js","sourceRoot":"","sources":["../../../src/utils/analytics.ts"],"names":[],"mappings":";;;AAAA,+CAAuC;AACvC,gDAG0B;AAC1B,+BAAoC;AACpC,MAAa,SAAS;IACZ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KAAK,CAAC,QAAQ,CAAC,MAAyC;QACtD,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxC,OAAO;QACT,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;YAClB,UAAU,EAAE,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,WAAW;YAC/C,KAAK,EAAE,uBAAuB;YAC9B,UAAU,EAAE;gBACV,MAAM;gBACN,IAAI,EAAE,IAAI,CAAC,IAAI;aAChB;SACF,CAAC,CAAC;QAEH,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC/B,CAAC;CACF;AArED,8BAqEC;AAEY,QAAA,SAAS,GAAG,IAAI,SAAS,EAAE,CAAC","sourcesContent":["import { PostHog } from 'posthog-node';\nimport {\n ANALYTICS_HOST_URL,\n ANALYTICS_POSTHOG_PUBLIC_PROJECT_WRITE_KEY,\n} from '../lib/constants';\nimport { v4 as uuidv4 } from 'uuid';\nexport class Analytics {\n private client: PostHog;\n private tags: Record<string, string | boolean | number | null | undefined> =\n {};\n private distinctId?: string;\n private anonymousId: string;\n\n constructor() {\n this.client = new PostHog(ANALYTICS_POSTHOG_PUBLIC_PROJECT_WRITE_KEY, {\n host: ANALYTICS_HOST_URL,\n flushAt: 1,\n flushInterval: 0,\n enableExceptionAutocapture: true,\n });\n\n this.tags = {};\n\n this.anonymousId = uuidv4();\n\n this.distinctId = undefined;\n }\n\n setDistinctId(distinctId: string) {\n this.distinctId = distinctId;\n this.client.alias({\n distinctId,\n alias: this.anonymousId,\n });\n }\n\n setTag(key: string, value: string | boolean | number | null | undefined) {\n this.tags[key] = value;\n }\n\n captureException(error: Error, properties: Record<string, unknown> = {}) {\n this.client.captureException(error, this.distinctId ?? this.anonymousId, {\n team: 'growth',\n ...this.tags,\n ...properties,\n });\n }\n\n capture(eventName: string, properties?: Record<string, unknown>) {\n this.client.capture({\n distinctId: this.distinctId ?? this.anonymousId,\n event: eventName,\n properties: {\n ...this.tags,\n ...properties,\n },\n });\n }\n\n async shutdown(status: 'success' | 'error' | 'cancelled') {\n if (Object.keys(this.tags).length === 0) {\n return;\n }\n\n this.client.capture({\n distinctId: this.distinctId ?? this.anonymousId,\n event: 'setup wizard finished',\n properties: {\n status,\n tags: this.tags,\n },\n });\n\n await this.client.shutdown();\n }\n}\n\nexport const analytics = new Analytics();\n"]}
|
package/package.json
CHANGED