Getting Started

These instructions will get you setup to use WebSDK in your project.

install using npm:

npm install @metrixorg/websdk

Initialize Metrix at the start of your application by calling the following method:

import { init } from '@metrixorg/websdk';

init(APP_ID, API_KEY, config)

Or add within <script> tag:
Place the following <script> inside <head> of your project.

<script src="https://box.backtory.com/metricx/sdk/web/metrix.umd-${VERSION}.js"></script>

Note: Replace ${VERSION} with desired version(e.g., 1.2.1). or to always use latest version replace ${VERSION} with latest.

Initialize Metrix at the start of your application by calling the following method:

typescript:

declare let Metrix: any;

Metrix.init(APP_ID, API_KEY, config);

javascript:

Metrix.init(APP_ID, API_KEY, config);
nametypedescriptionrequired
APP_IDstringYour application identifier. You can find this id in your Metrix dashboard under Settings > App Info.true
API_KEYstringYou can find this key in dashboard.true
configObjectconfig to enabling PUSH_SDK and track user locationfalse

API

authorizeUser

To authorize user to Metrix server, you must call this API.

authorizeUser(username: string) : void
Parameter NameParameter Typedescriptionrequired
usernamestringname of user to authorize to metrix serverstrue

newEvent

Each interaction that the user has with your application can be introduced as an Event in your dashboard and application in order for Metrix to collect and present its statistics.


newEvent(slug: string, customAttributes: {[key: string]: string}): void
nametypedescriptionrequired
slugstringgenerated event slug in dashboardtrue
customAttributes{[key: string: string]}any custom attribute related to eventfalse

You can use Metrix to track any events in your application. Suppose you want to track every tap on a button. You would have to create a new event in the Events Management section of your dashboard (Settings > Events > Add event) and retrieve the generated slug for the event. The slug is to be used in the application code to send the event to Metrix library. So In your button's onClick method you could then invoke the Metrix newEvent method providing the event slug and optionally some attributes named customAttributes related to the event like the following:

// 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';

newEvent('EVENT_SLUG', attributes);

setCustomAttribute

setCustomAttribute(key: string, value: string) : void
Parameter NameParameter Typedescriptionrequired
keystringkey of custom attributetrue
valuestringvalue of custom attributetrue

setCustomAttribute

setCustomUserId(customId: string) : void
Parameter NameParameter Typedescriptionrequired
customIdstringuser custom IDtrue

setFirstName

you can use metrix WebSDK to store user session visiting your application. By this method you can send user first name to metrix library.

setFirstName(firstName: string) : void
Parameter NameParameter Typedescriptionrequired
firstNamestringuser first nametrue

setLastName

By this method you can send user last name to metrix library.

setLastName(lastName: string) : void
Parameter NameParameter Typedescriptionrequired
lastNamestringuser first nametrue

setEmail

By this method you can send user email to metrix library.

setEmail(email: string) : void
Parameter NameParameter Typedescriptionrequired
emailstringuser emailtrue

setHashedEmail

By this method you can send user hashed email to metrix library.

setHashedEmail(hashedEmail: string) : void
Parameter NameParameter Typedescriptionrequired
hashedEmailstringuser hashed emailtrue

setPhoneNumber

By this method you can send user phone number to metrix library.

setPhoneNumber(phoneNumber: string) : void
Parameter NameParameter Typedescriptionrequired
phoneNumberstringuser phone numbertrue

setHashedPhoneNumber

By this method you can send user phone number as hashed phone number to metrix library.

setHashedPhoneNumber(hashedPhoneNumber: string) : void
Parameter NameParameter Typedescriptionrequired
hashedPhoneNumberstringuser hashed phone numbertrue

setCountry

By this method you can send user country to metrix library.

setCountry(country: string) : void
Parameter NameParameter Typedescriptionrequired
countrystringuser countrytrue

setCity

By this method you can send user city to metrix library.

setCity(city: string) : void
Parameter NameParameter Typedescriptionrequired
citystringuser citytrue

setRegion

By this method you can send user city to metrix library.

setRegion(region: string) : void
Parameter NameParameter Typedescriptionrequired
regionstringuser regiontrue

setLocality

By this method you can send user locality to metrix library.

setLocality(locality: string) : void
Parameter NameParameter Typedescriptionrequired
localitystringuser localitytrue

setGender

By this method you can send user gender to metrix library.

setGender(gender: string) : void
Parameter NameParameter Typedescriptionrequired
genderstringuser gendertrue

setBirthday

By this method you can send user birthday date to metrix library.

setBirthday(birthday: string) : void
Parameter NameParameter Typedescriptionrequired
birthdaystringuser birthdaytrue

Web Push

To enabling web push, follow these steps:

Step 1: Add Service Worker File (only for sites with HTTPS protocol)

download this file and put it at root of your project files ( this means somewhere beside index.html or index.php file ).

Note: this file must be accessible with end of your site domain URL. for example if domain is https://dashboard.metrix.ir this file must be accessible with https://dashboard.metrix.ir/metrix-sw.js.

Step 2: Enable Push config

pass this config as third parameter to init method.

import { init } from '@metrixorg/websdk';

init(APP_ID, API_KEY, config)

PUSH initialization config:

{
  push: {
    enabled : true; // defaults to false but if set to true you must provide publicKey.
    publicKey: YOUR_PUBLIC_KEY; // your push subscription public key.
  }
}

Additional Features

Session Tracking (comming soon)

Metrix, tracks sessions visiting your website/application and collect data from their activity. types of activity are:

  • duration: session duration time, the time that user is active on your website/application tab by specified session.

    note: The time when the user leaves your website/application tab is not counted as session duration. Therefore, when the user returns to your website/application tab, the duration of the session is calculated from that moment.

  • activityFlow: array of pages address of your website/application that the user has viewed.

Metadata

Metrix, collect metadata from user device, browser and location of user visiting your website/application.

data that is collect from device is:

  • os
  • osVersion
  • deviceLang
  • screen

data that is collect from browser is:

  • browserName
  • browserVersion
  • timezone
  • timezoneOffset

location data is:

  • latitude
  • longitude