Log your first custom event
How to log custom events to Statsig from client and server SDKs, including event names, values, and metadata used for metrics and analytics.
logEvent API in any Statsig client or server SDK. You can then view the event in Metrics Explorer, turn it into custom metrics, visualize it in dashboards, or use it in experiment results to show the impact of your experiments.Statsig provides several ways to get started:
- Statsig web SDKs offer autocaptured web analytics to automatically log common events like pageviews, clicks, and more.
- Statsig offers integrations to connect your existing event data in Segment, mParticle, and other sources.
- All Statsig client and server SDKs provide a simple
logEventAPI to instrument your own events in your app or webserver.
For general guidance on event logging and core concepts, continue reading or go to the "Logging events through SDKs" section.
Identifying users with the StatsigUser object
Many analytics platforms have a concept of "identifying" a user. In Statsig, the StatsigUser object represents this concept. You set the StatsigUser object at initialization time in client SDKs, or with each event in server SDKs.
StatsigUser is a set of properties that describe the user. The JSON definition is consistent across all SDKs and integrations:{
"userID": "123",
"customIDs": {},
"email": "user@example.com",
"ip": "192.168.1.1",
"userAgent": "Mozilla/5.0...",
"country": "US",
"locale": "en-US",
"appVersion": "1.0.0",
"custom": {},
"privateAttributes": []
}
Statsig reserves the userID field for a unique identifier for the user. Use the ID of the logged-in user.
The Group analytics section explains customIDs.
The other fields support targeting and evaluation for feature flags, and you can also use them for custom metrics and data queries in Metrics Explorer.
Group analytics
Another core concept in analytics is grouping. A user can belong to a company, organization, page, or other entity.
In Statsig, the customIDs field represents groups. It's a dictionary that can contain multiple IDs for a single user. For example, a user can have the following customIDs:
{
"userID": "123",
"customIDs": {
"companyID": "456",
"projectID": "abc"
}
}
Statsig computes all metrics at the user level and also for each custom identifier. This means you can run experiments at a company level, where all users in a company receive the same experience, and compare the impact on company-level metrics.
You can also use Metrics Explorer to slice metrics at the company level rather than the user level, if you have configured a customID for the group. For example, the Statsig project can display console page views at a user or company level:

To set up a new "group" or "customID":
- Go to your project settings.
- Locate the "Custom IDs" section and hit "edit".

- Name the new
customIDand add a description so other members of your project understand its purpose. - Start logging events with that customID in the
customIDsfield of the StatsigUser object.
You must provide the set of all IDs on each StatsigUser object.
Best practices
Set all known IDs on each StatsigUser object
To generate metrics for each user and group, Statsig requires all IDs on each StatsigUser object. Pass all identifiers for the user at client SDK initialization time or when calling logEvent in a server SDK, so each identifier contributes to the correct metrics.
If you use a logged-out identifier for anonymous users, continue passing that identifier after the user logs in and you populate userID. Passing the logged-out identifier after login lets gates or experiments targeting that identifier continue bucketing the user correctly. It also lets Statsig calculate metrics at both the logged-out and logged-in levels.
Deduplicating custom events
Statsig doesn't provide a way to deduplicate custom events that you log.
Naming conventions
Consistency is the most important aspect of event naming. An inconsistent events catalogue becomes difficult to navigate. Statsig accepts any naming convention (for example, Page View, PageView, Page-view, or page_view), but the recommended format is page_view: lowercase, no spaces. You can't use special characters in event names. Statsig drops events that contain the following character set: "\\[\]{}<>#=;&$%|\u0000\n\r". Using the recommended convention avoids duplicate entries with different casing and ensures compatibility with downstream systems connected through integrations.
What to measure
For experimentation and product analytics, log events that correspond to user actions. How you classify events depends on your goals:
- Avoid high-cardinality event names if you have a small user base, because sparse data reduces the reliability of metrics. For example, an eCommerce website should use a generic
product_page_viewevent rather than a separate event name for every product page. Store contextual page information in theoptional_event_metadataobject. - If you have a well-defined user funnel, log each step as a separate event.
- If you have a less defined user journey, log generic events (for example,
page_view) and add more detail to the value field (the primary dimension of interest) or theoptional_event_metadataobject.
Logging events through SDKs
All Statsig SDKs provide an API to log events. The API looks like this:
statsig.logEvent(
event_name,
optional_event_value,
optional_event_metadata
);
Where event name describes a notable event in your product, like sign_up, achievement_unlocked, add_to_cart, check_out, etc.
Events can optionally include an event value: for example, the type of achievement unlocked, the name of the product added to cart, or the price of the item purchased. In experiments, the value field generates an automatic breakdown of the event when there are fewer than 8 distinct values.
The metadata field lets you specify additional details about the event. The following examples show how a logEvent call looks:
statsig.logEvent(
'add_to_cart', // Name
19.99, // Price
{
item_id: 'BC22010',
cart_size: '2',
user_segment: 'first_time_purchaser',
}
);
Size limits on event payload There are limits to how large each event field can be:
| Field type | Limit |
|---|---|
| Object fields | 4096 (stringified length) |
| String fields | 64 (length) |
Was this helpful?