Skip to content

JQ and Orbee Context

The JS SDK provides context to model different parts of state, events, and common browser variables. Orbee also provides a query language for navigating this context in an easy, digestible format, especially when modeling parameters in JSON, rather than JavaScript.

Orbee Context

The Context is a way to provide a query-able format to all the major variables within the JS SDK. The below table maps out those variables, objects, and fields.

Key Type Description
$.event EventSpecWrapper or other the current event object, or datalayer input
$.state State the currently stored state in the datalayer
$.url URL the current URL
$.referrer URL the current page referrer URL

Core defined structures

There are core defined structures that are used within the JS SDK, and provided through the context variables. Each are detailed below.

Event Wrapper

{
    type: "",      // the event type
    properties: {}  // the properties added to the event
}

Events are added to the datalayer (merged) in this format:

{
    event: eventType, // eventType is `state.event`
    ...eventProperties // all properties are available as `state.*`
}

State

{
    isNewVisit: false,
    isNewVisitor: false,
    optOut: {
        optedOut: false
    },
    user: {
        id: ''
    },
    pageview: {
        id: '',
        page: {
            url: URL{},     // document.location.href
            referrer: URL{} // document.referrer
        }
    },
    visit: {
        id: '',
        originalURL: URL{},
        originalReferrer: URL{},
        source: '',
        medium: '',
        campaign: '',
        stats: {
            firstSeenTstamp: '',
            currentVisitTstamp: '',
            lastVisitTstamp: '',
            visitCount: 4
        }
    },
    ...the rest of our datalayer variables
}

URL

{
    scheme: 'http',
    host: 'google.com',
    port: '',
    path: '/search',
    query: {
        q: 'testing query'
    },
    fragment: '',
    raw: 'http://google.com/search?q=testing%20query'
}

JQ

JQ is a simple query language for querying JSON objects in JavaScript. Some simple examples are below (all examples use $, Orbee's context variable):

$.event.eventModel.label
$.event.event
$.url.path
$.event.eventModel.elements.*.id
$.event.eventModel.elements.0

The language utilizes JavaScript's simple usage of objects and arrays -- they both support the [] lookup syntax:

var a = {b: 2}

console.log(a['b']); // 2

var c = [4];

console.log(c[0]); // 4
console.log(c['0']); // 4

Last update: 2023-04-19