Skip to Content
AI Powered Docs! 🤖 These docs are translated with AI, so keep an eye out for minor quirks. We're always improving!
TechnicalWebTracking Events

Sending Events (Attribution & Automation Services)

Any interaction that users have with your website can be recorded as an (event) in the Metrix dashboard. Metrix collects these events and provides analytical insights for you.

To send events to Metrix servers, you can use one of the following methods:

Method 1: Sending an Event by Slug

This method sends custom events to Metrix using the event slug defined in your dashboard.

newEvent(slug: string, customAttributes: {[key: string]: string}, onsuccess: () => void): void
NameTypeDescriptionRequired
slugstringThe event slug created in the Metrix dashboard.Yes
customAttributes{[key: string]: string}Any desired attribute related to the specific event can be sent in customAttributes.No
onsuccess() => voidA callback function executed after the event is successfully sent.No

Using Metrix, you can track any event or occurrence on your website. For example, suppose you want to track clicks on a button on your website. For this purpose, you must first create a specific event for this occurrence in the Events Management section of the Metrix dashboard. (Settings -> Manage Events -> Create event). Then, use the generated slug for this event in your website code to send the event. Therefore, in the onclick method for your desired button, you should call the Metrix newEvent method with the created event’s slug and, optionally, along with some customAttribute as follows:

// Send simple event import { newEvent } from '@metrixorg/websdk'; newEvent('EVENT_SLUG'); // Send an event with custom attribute const attributes = {}; attributes['first_name'] = 'Ali'; attributes['last_name'] = 'Bagheri'; attributes['manufacturer'] = 'Nike'; attributes['product_name'] = 'shirt'; attributes['type'] = 'sport'; attributes['size'] = 'large'; attributes['purchase_date'] = '2024-11-20T11:24:03.000Z'; // use ISO 8601 to consider this attribute as s a date newEvent('EVENT_SLUG', attributes);

If the event slug is empty, the following error will occur:

Error: newEvent: slug is required and cannot be empty.

Method 2: Sending an Event by Name

This method sends a custom event to Metrix using the event name defined in the dashboard.

newEventByName( name: string, customAttributes: {[key: string]: string}, onsuccess: () => void): void
NameTypeDescriptionRequired
namestringThe Event Name created in the Metrix dashboard for the intended eventYes
customAttributes{[key: string]: string}Any desired attribute related to the specific event can be sent in customAttributes.No
onsuccess() => voidA callback function executed after the event is successfully sent.No

Similar to the method of creating an event with a slug, you must create the desired event in the settings section of the Metrix dashboard. Then, use the generated Event Name for this event in your website code to send the event. Therefore, in the onclick method for your desired button, you should call the newEventByName method as follows:

// Send simple event import { newEventByName } from '@metrixorg/websdk'; newEventByName('PURCHASE_COMPLETED'); // Send an event with custom attribute const attributes = {}; attributes['first_name'] = 'Ali'; attributes['last_name'] = 'Bagheri'; attributes['manufacturer'] = 'Nike'; attributes['product_name'] = 'shirt'; attributes['type'] = 'sport'; attributes['size'] = 'large'; attributes['purchase_date'] = '2024-11-20T11:24:03.000Z'; // use ISO 8601 to consider this attribute as s a date newEventByName('PURCHASE_COMPLETED', attributes);

If the event name is empty, the following error will occur:

Error: newEventByName: name is required and cannot be empty.

Creating a Revenue Event (Attribution Service Only)

You can record a revenue event using the following method. First, log into the Metrix dashboard and create a revenue event from the Manage Events section. Then, use the generated event slug in your application:

newRevenue('EVENT_SLUG', 12000, 'IRR');

The inputs of the newRevenue method are as follows:

NameTypeDescriptionRequired
slugstringThe event slug created in the Metrix dashboard.Yes
revenuenumberThe revenue amountYes
currencyIRR (Default) or USD or EURCurrency unitNo
Last updated on