---
title: Event Tracking
description: Track your customers' key-value metrics on a per customer basis.
---

Event tracking products are difficult to test in the field and are filled with feature bloat. Our new event tracking tool lets you easily capture your products key-value metrics on a per customer basis. We’ll stitch together your event data with your customer subscription data for you, to give you a comprehensive view of your customers.

## Customer Events

Events are created through a simple HTTP request.

### Server Side

For sending server side events, include your Churnkey API key as a header, as shown below. Get your API key and App ID from [Churnkey | Account](https://app.churnkey.co/account).

```jsx
const headers = {
  'x-ck-api-key': 'YOUR_CHURNKEY_API_KEY',
  'x-ck-app': 'YOUR_CHURNKEY_APP_ID',
};

const eventBody = {
  event: 'Post Created',
  customerId: 'cus_abc', // from billing provider e.g. Stripe

  ////////////////////////
  // OPTIONAL ITEMS BELOW
  ////////////////////////
  uid: 'user_xyz', // unique id of the customer e.g. your database id
  eventDate: '2022-05-01', // use for backfilling data, default is now
  eventData: {
    postType: 'image',
  },

  ////////////////////////
  // FOR B2B PRODUCTS
  ////////////////////////
  user: {
    uid: 'user_unique_id', // e.g. your database id for the user
  },
};

axios.post('https://api.churnkey.co/v1/api/events/new', eventBody, headers);
```

**A Quick Note on Naming Convention**

To keep event naming consistent across your application, we recommend using an [object-action](https://segment.com/academy/collecting-data/naming-conventions-for-clean-data/) convention, capitalized and with a regular space.

- “Product Clicked”
- “Application Opened”
- “Account Created”
- “User Registered”

### Server Side Bulk Create

**Ideal for backfilling data for training Churnkey customer health models.** You can create up to 100 events in one API request using our bulk event endpoint. Please note that, unlike with single event creation, you cannot include customer and user data updates with the bulk event endpoint.

```jsx
const headers = {
  'x-ck-api-key': 'YOUR_CHURNKEY_API_KEY',
  'x-ck-app': 'YOUR_CHURNKEY_APP_ID',
};

const events = [
  {
    // ...events as defined above, including event, eventDate, and customerId or uid
  },
];

axios.post('https://api.churnkey.co/v1/api/events/bulk', events, headers);
```

### Client Side

::alert{type="warning" :emoji="💡"}
Server side event creation is preferred

::

Client side events can be authenticated by signing the `customerId` or `customerEmail` if the `customerId` is not available. Events can also be created without authentication, and will be marked as “unverified” accordingly.

For details on creating the `hmacSignature` , see Step 2 on our Cancel Flow [Installation Guide](/installing-churnkey).

```jsx
// Optional. See Step 2 of our Installation Guide
const hmacSignature = await getSignedCustomerId(customerId); // Sign customerEmail if customerId is not available

window.churnkey.event({
  customerId: 'cus_a23',
  customerEmail: 'jane@example.com',
  appId: 'YOUR_CHURNKEY_APP_ID',
  authHash: hmacSignature, // Optional
  event: 'Post Created',
  eventData: {
    postType: 'image',
  },
});
```

## Customer Attributes

In addition to events, you can update customer data. This can be done by including a `customerData` property in the create (single) event endpoint, or by sending an explicit request to `events/customer-update`.

You must include at least one of `uid` or `customerId`. For B2B products, you can also pass in a `user` property as shown below to update user data records.

```jsx
const headers = {
  'x-ck-api-key': 'YOUR_CHURNKEY_API_KEY',
  'x-ck-app': 'YOUR_CHURNKEY_APP_ID',
};

const dataUpdate = {
  uid: 'unique_id', // unique id of the customer e.g. your database id
  customerId: 'cus_xyz', // from billing provider e.g. Stripe
  customerData: {
    numSeats: 7,
    name: 'Acme, Inc.',
  },

  ////////////////////////
  // FOR B2B PRODUCTS
  ////////////////////////
  user: {
    uid: 'user_unique_id', // e.g. your database id for the user
    data: {
      name: 'John Henry',
      email: 'johnhenry@example.com',
    },
  },
};

axios.post('https://api.churnkey.co/v1/api/events/customer-update', dataUpdate, headers);
```
