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/eventvisorThe generated TypeScript code will be found under the ./src/eventvisor directory.
At this moment, only TypeScript is supported. More languages will be supported as Eventvisor starts supporting more languages via new SDKs.
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 { createInstance } from "@eventvisor/sdk";import { setInstance } from "./eventvisor"; // new generated codeconst eventvisor = createInstance({ 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";setAttribute("userId", "user-123");setAttribute("deviceId", "device-123");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.

