Eventvisor

Advanced

Code generation

Eventvisor CLI ships with a command for generating code from your already defined events and attributes.

Command

Run this from the root directory of your Eventvisor project:

Command
$ npx eventvisor generate-code \
--language=typescript \
--out-dir=src/eventvisor

The generated TypeScript code will be found under the ./src/eventvisor directory.

Filter generated events and attributes with repeatable tags or targets:

npx eventvisor generate-code \
--language typescript \
--out-dir src/eventvisor \
--target checkout \
--target account

Code generation currently produces TypeScript definitions. Runtime SDKs are available for JavaScript and Java, and other generators can be added when they provide clear value for an SDK.

You may either:

  • copy the generated code to your application(s), or
  • publish it as a reusable package (privately)

Usage

Set instance

Create and set the Eventvisor SDK instance first:

your-app/src/index.ts
import { createEventvisor } from "@eventvisor/sdk";
import { setInstance } from "./eventvisor"; // new generated code
const eventvisor = createEventvisor({
datafile: { ... },
});
setInstance(eventvisor);

Import type-safe functions

Now you can import the type-safe functions from previously generated code:

your-app/src/index.ts
import { setAttribute, track } from "./eventvisor";
await setAttribute("userId", "user-123");
await setAttribute("deviceId", "device-123");
await track("pageView", {
url: "https://www.yoursite.com/home",
});

TypeScript will automatically infer the correct types for the set attributes and tracked events based on the definitions in your project.

Reusable Schema references are resolved before types are generated, including transitive and nested references.

Events without payload properties

An event with a closed, empty object schema:

events/applicationStarted.yml
description: Application started
tags:
- web
type: object

Generates this type:

export type ApplicationStartedEvent = Record<string, never>;

Adding additionalProperties: true creates an open object instead:

events/applicationMetadata.yml
description: Application metadata
tags:
- web
type: object
additionalProperties: true
export type ApplicationMetadataEvent = Record<string, unknown>;

Both types accept an empty object, so their generated tracking calls can omit the second argument:

await track("applicationStarted");
await track("applicationMetadata");
await track("applicationMetadata", {
releaseChannel: "stable",
});

The closed event rejects additional properties. Generated tracking calls still require a value for events whose schemas contain required properties.

Previous
Monorepo