Using Identities

When you want to send an event, you have to provide at least one identity - the session id. Usually, when a visitor arrives at your website or your app, you will not know them until they log in, but you can generate an anonymous id to recognize them during their whole journey. You will need to store this id locally (in a cookie, or in the local storage of your app), and then send it with every event through the user.identities.sessionId field in the activity object.

Using the SDK features

Setting identities

If you are using the Javascript SDK, then a random session id is created for the visitor and automatically stored in local storage as attraqtsessionid. This session id is then attached to any event being sent through the library.

If you have a different session id that's generated elsewhere in your system and want to use that one instead, you can add it through the addUserIdentity method like so:

xo.activity.addUserIdentity('sessionId', 'f49c75a2-6994-41e2-875d-97ec4d19254b');

Once the shopper logs in, you will know exactly who they are through the id you have for them in your system, for example an e-commerce id. The e-commerce id can then be added to subsequent events along with the already existing session id.

The same command can be used to add a different identity i.e. the e-commerce id.

xo.activity.addUserIdentity('ecommerceId', '0ca79556-2fee-11eb-adc1-0242ac120002');

You can also add multiple identities at the same time(and overwrite any preexisting ones added by addUserIdentity, including the default sessionId), through the setUserIdentities method like so:

const identities = { 
    'ecommerceId': '0ca79556-2fee-11eb-adc1-0242ac120002', 
    'sessionId': 'f49c75a2-6994-41e2-875d-97ec4d19254b' 
};

xo.activity.setUserIdentities(identities);

Important! Even though you are now sending the e-commerce id, the session id still has to be provided. If you're using the SDK, this will be already handled for you.

Happy flow example

  • User arrives on your website - the SDK assigns a default session id and stores it in local storage.

  • User performs various different actions - the SDK attaches the session id to any activity event you send out through the SDK.

  • User logs-in - you retrieve the user's e-commerce id from the log-in and attach it to all subsequent events sent through calling xo.activity.addUserIdentity('ecommerceId','the-id-of-the-user')

  • User performs more different actions - the SDK attaches both the newly added e-commerce id as well as the already existing session id to any activity event you send out. On the first activity sent this way, we match the session id with the e-commerce id, so all previous actions are now assigned to the same user.

Setting segments and traits

Apart from identities, the SDK is also able to handle segments and traits, so it makes it very convenient for you to send them. This is specific to the XO platform.

To set a user segment, you need to call the addUserSegment method.

xo.activity.addUserSegment('new');

To set multiple segments at once(and overwrite any preexisting ones set by addUserSegment), you can use the setUserSegments method.

const segments = ['new', 'prospect'];

xo.activity.setUserSegments(segments);

Similarly, to add a trait for the user, you need to call the addUserTrait method.

xo.activity.addUserTrait('age', '45');

To set all traits for the user at once(and overwrite any preexisting ones set by addUserTrait), you can use the setUserTraits method.

var traits = {
    age: 45,
    buyer: true
};

xo.activity.setUserTraits(traits);

Important! Traits are information you know on your users and that you want to use in Attraqt's platform. You must only send information relevant to the platform usage and avoid sending personal information.

Setting the complete user object

You also have the ability to add the complete user object yourself, and override any preexisting configuration, through the setUser method like so:

const user = {
    'identities': {
        'sessionId': 'f49c75a2-6994-41e2-875d-97ec4d19254b',
    },
    'segments': ['new']}
}

attraqt.activity.setUser(user)

Logging out

If your user logs out, then at that point it doesn't make sense to pass his information any more. To do this you can use the clearUser method.

xo.activity.clearUser();

This method clears the user object, and the session id in storage if handled by the SDK.

If you are using a SPA or a PWA, and the browser is not reloading, then this method is mandatory when the user logs out.

Sending the object with every activity (Necessary for the REST API)

If you are using the REST API to send events, or if you would prefer to completely manage the user object yourself, you can do that by attaching the user object as part of the activity object every time you send an activity.

const activity = {
  "action": "view",
  "target": {
    "product": "123"
  },
  "user": {
    "identities": {
      "sessionId": "f49c75a2-6994-41e2-875d-97ec4d19254b",
      "ecommerceId":"0ca79556-2fee-11eb-adc1-0242ac120002"
    },
    "segments": ["new", "prospect"],
    "traits": {
      "age": 45,
      "buyer": true
    }
  }
};

xo.activity.send(activity);

Last updated