Skip to content

Commit e9d7de3

Browse files
fix(tracking-plan): identifyTraits must have payload (#805)
* refactor(tracking-plan): identifyTraits must have payload * test: update tests
1 parent a78ba11 commit e9d7de3

3 files changed

Lines changed: 20 additions & 25 deletions

File tree

packages/tracking-plan/src/index.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,23 @@ function getTelemetryEventNames(
5959
function buildInMemorySource(
6060
originalSource: ts.SourceFile,
6161
eventTypeNames: string[],
62-
identifyTraitsName: string,
6362
commonPropertiesName?: string,
6463
): ts.SourceFile {
6564
// Appends "Resolved*" type aliases that flatten intersections and type references
6665
// into basic types. The TypeChecker then emits fully-expanded, readable types
6766
// for each event property in the tracking plan.
67+
// All events (including identity events) must have a `payload` field.
6868
const resolvedEvents = eventTypeNames
6969
.map(
7070
(name) => `
7171
type Resolved${name} = {
7272
name: ${name}['name'];
73-
payload: ResolveType<${name} extends { payload: infer P } ? P : Record<string, never>>;
73+
payload: ResolveType<${name}['payload']>;
7474
};`,
7575
)
7676
.join('\n');
7777

7878
const resolvedSections = [
79-
`type Resolved${identifyTraitsName} = ResolveType<${identifyTraitsName}>;`,
8079
commonPropertiesName
8180
? `type Resolved${commonPropertiesName} = ResolveType<${commonPropertiesName}>;`
8281
: '',
@@ -305,7 +304,6 @@ function renderSection(
305304
function generateMarkdown(
306305
config: GenerateTrackingPlanConfig,
307306
events: EventInfo[],
308-
identifyTraits: SectionInfo,
309307
commonProperties?: SectionInfo,
310308
): string {
311309
const lines: string[] = [];
@@ -333,7 +331,6 @@ function generateMarkdown(
333331
if (commonProperties) {
334332
lines.push('- [Common Properties](#common-properties)');
335333
}
336-
lines.push('- [Identity](#identity)');
337334
for (const cat of sortedCategories) {
338335
lines.push(`- [${cat}](#${slugify(cat)})`);
339336
for (const event of byCategory.get(cat)!) {
@@ -346,8 +343,6 @@ function generateMarkdown(
346343
renderSection('Common Properties', commonProperties, lines);
347344
}
348345

349-
renderSection('Identity', identifyTraits, lines);
350-
351346
for (const cat of sortedCategories) {
352347
lines.push('');
353348
lines.push(`## ${cat}`);
@@ -378,22 +373,15 @@ export function generateTrackingPlan(
378373
);
379374

380375
const unionTypeName = 'TelemetryEvent';
381-
const identifyTraitsName = 'IdentifyTraits';
382376
const commonPropertiesName = 'CommonEventProperties';
383377
const eventTypeNames = getTelemetryEventNames(originalSource, unionTypeName);
384378
const inMemorySource = buildInMemorySource(
385379
originalSource,
386380
eventTypeNames,
387-
identifyTraitsName,
388381
commonPropertiesName,
389382
);
390383
const checker = createChecker(inMemorySource);
391384

392-
const identifyTraits = parseSectionType(
393-
identifyTraitsName,
394-
inMemorySource,
395-
checker,
396-
);
397385
const hasCommonProperties = originalSource.statements.some(
398386
(n) =>
399387
(ts.isTypeAliasDeclaration(n) || ts.isInterfaceDeclaration(n)) &&
@@ -407,5 +395,5 @@ export function generateTrackingPlan(
407395
parseTelemetryEvent(name, inMemorySource, checker),
408396
);
409397

410-
return generateMarkdown(config, events, identifyTraits, commonProperties);
398+
return generateMarkdown(config, events, commonProperties);
411399
}

packages/tracking-plan/test/fixtures/telemetry-events.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ export interface CommonEventProperties {
44
session_id: string;
55
}
66

7-
/** Traits sent with identify calls. */
8-
export interface IdentifyTraits {
9-
/** Unique identifier for the device. */
10-
device_id: string;
11-
/** The operating system platform. */
12-
platform: string;
13-
}
7+
/**
8+
* Fired when a user is identified.
9+
* @category Identity
10+
*/
11+
export type IdentifyEvent = {
12+
name: 'Identify';
13+
payload: {
14+
/** Unique identifier for the device. */
15+
device_id: string;
16+
/** The operating system platform. */
17+
platform: string;
18+
};
19+
};
1420

1521
/**
1622
* Fired when a connection to the server is established.
@@ -50,4 +56,5 @@ export type ApplicationLaunchedEvent = {
5056
export type TelemetryEvent =
5157
| ConnectionEvent
5258
| QueryExecutedEvent
53-
| ApplicationLaunchedEvent;
59+
| ApplicationLaunchedEvent
60+
| IdentifyEvent;

packages/tracking-plan/test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ describe('TrackingPlan', function () {
4545
assert.ok(result.includes('Do not edit manually'));
4646
});
4747

48-
it('renders the Identity section with trait properties', function () {
49-
assert.ok(result.includes('## Identity'));
48+
it('renders the identify event alongside other events with its payload properties', function () {
49+
assert.ok(result.includes('### Identify'));
5050
assert.ok(result.includes('`device_id`'));
5151
assert.ok(result.includes('`platform`'));
5252
});

0 commit comments

Comments
 (0)