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.

Previous
Monorepo