Skip to content
Open
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
18 changes: 3 additions & 15 deletions packages/tracking-plan/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,23 @@ function getTelemetryEventNames(
function buildInMemorySource(
originalSource: ts.SourceFile,
eventTypeNames: string[],
identifyTraitsName: string,
commonPropertiesName?: string,
): ts.SourceFile {
// Appends "Resolved*" type aliases that flatten intersections and type references
// into basic types. The TypeChecker then emits fully-expanded, readable types
// for each event property in the tracking plan.
// All events (including identity events) must have a `payload` field.
const resolvedEvents = eventTypeNames
.map(
(name) => `
type Resolved${name} = {
name: ${name}['name'];
payload: ResolveType<${name} extends { payload: infer P } ? P : Record<string, never>>;
payload: ResolveType<${name}['payload']>;
};`,
)
.join('\n');

const resolvedSections = [
`type Resolved${identifyTraitsName} = ResolveType<${identifyTraitsName}>;`,
commonPropertiesName
? `type Resolved${commonPropertiesName} = ResolveType<${commonPropertiesName}>;`
: '',
Expand Down Expand Up @@ -305,7 +304,6 @@ function renderSection(
function generateMarkdown(
config: GenerateTrackingPlanConfig,
events: EventInfo[],
identifyTraits: SectionInfo,
commonProperties?: SectionInfo,
): string {
const lines: string[] = [];
Expand Down Expand Up @@ -333,7 +331,6 @@ function generateMarkdown(
if (commonProperties) {
lines.push('- [Common Properties](#common-properties)');
}
lines.push('- [Identity](#identity)');
for (const cat of sortedCategories) {
lines.push(`- [${cat}](#${slugify(cat)})`);
for (const event of byCategory.get(cat)!) {
Expand All @@ -346,8 +343,6 @@ function generateMarkdown(
renderSection('Common Properties', commonProperties, lines);
}

renderSection('Identity', identifyTraits, lines);

for (const cat of sortedCategories) {
lines.push('');
lines.push(`## ${cat}`);
Expand Down Expand Up @@ -378,22 +373,15 @@ export function generateTrackingPlan(
);

const unionTypeName = 'TelemetryEvent';
const identifyTraitsName = 'IdentifyTraits';
const commonPropertiesName = 'CommonEventProperties';
const eventTypeNames = getTelemetryEventNames(originalSource, unionTypeName);
const inMemorySource = buildInMemorySource(
originalSource,
eventTypeNames,
identifyTraitsName,
commonPropertiesName,
);
const checker = createChecker(inMemorySource);

const identifyTraits = parseSectionType(
identifyTraitsName,
inMemorySource,
checker,
);
const hasCommonProperties = originalSource.statements.some(
(n) =>
(ts.isTypeAliasDeclaration(n) || ts.isInterfaceDeclaration(n)) &&
Expand All @@ -407,5 +395,5 @@ export function generateTrackingPlan(
parseTelemetryEvent(name, inMemorySource, checker),
);

return generateMarkdown(config, events, identifyTraits, commonProperties);
return generateMarkdown(config, events, commonProperties);
}
23 changes: 15 additions & 8 deletions packages/tracking-plan/test/fixtures/telemetry-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ export interface CommonEventProperties {
session_id: string;
}

/** Traits sent with identify calls. */
export interface IdentifyTraits {
/** Unique identifier for the device. */
device_id: string;
/** The operating system platform. */
platform: string;
}
/**
* Fired when a user is identified.
* @category Identity
*/
export type IdentifyEvent = {
name: 'Identify';
payload: {
/** Unique identifier for the device. */
device_id: string;
/** The operating system platform. */
platform: string;
};
};

/**
* Fired when a connection to the server is established.
Expand Down Expand Up @@ -50,4 +56,5 @@ export type ApplicationLaunchedEvent = {
export type TelemetryEvent =
| ConnectionEvent
| QueryExecutedEvent
| ApplicationLaunchedEvent;
| ApplicationLaunchedEvent
| IdentifyEvent;
4 changes: 2 additions & 2 deletions packages/tracking-plan/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ describe('TrackingPlan', function () {
assert.ok(result.includes('Do not edit manually'));
});

it('renders the Identity section with trait properties', function () {
assert.ok(result.includes('## Identity'));
it('renders the identify event alongside other events with its payload properties', function () {
assert.ok(result.includes('### Identify'));
assert.ok(result.includes('`device_id`'));
assert.ok(result.includes('`platform`'));
});
Expand Down
Loading