Using Global Context API

Updated 3 months ago by Michael Alon

Global Context

The Aptrinsic Web SDK supports the ability to set a "Global Context" for all user events captured by the Aptrinsic tracking tag.  Setting the "context" will automatically attach/assign those properties/values to all auto-tracked events (including custom events). 

This functionality will help your organization to :

  • Establish a sustainable instrumentation strategy 
  • Gain greater visibility 
  • Add context to the auto-tracked events that are specific to your organization

 The global context can be defined as a list of property/value pairs with the following types supported:

Here is an example call:

// Setting Global Context

aptrinsic('set', 'globalContext', {"projectId":12345,"Project Type" :"Work Order", "Project Date":"2018-03-08T18:11:00Z" });

After this call all future events will be populated with global context: projectId=12345, Project Type = "Work Order" etc.

To update an existing value just call set again with the new value and all future events will now use the new value:

// Setting Global Context (Update a property)

aptrinsic('set', 'globalContext', {"projectId":67890});

In the above example, the global context value for key "projectId" will now be 67890 for all future events.

But in the case we want to override only if not set before, you can call like this:

aptrinsic('set', 'globalContext', {"projectId" : 67890});
aptrinsic('setOnce', 'globalContext', {"projectId" : 45673})

In the above calls, the global context for key "projectId" stays at 67890 and is not overridden by the second call

Remove

Removes a list of global context. This will remove the global context from being populated in future  events but does not effect past events

aptrinsic('remove', 'globalContext', ["projectId"])

The above call will no longer include the "projectId" property on future events. 

And finally, one more example:

aptrinsic('set', 'globalContext', {"projectId" : 67890});
//call custom event
aptrinsic('track', 'purchased', {"amount" : 1});
aptrinsic('remove', 'globalContext', ["projectId"]);
//call the same custom event again with different value
aptrinsic('track', 'purchased', {"amount" : 2});

In the above example, the first call will assign "projectId=67890" to the "purchased" custom event , but it will not be included as part of the second call


How did we do?