Enabling Global Context

I was trying to enable global context in my javascript tacker with the below sample.

let user_context = {
schema: 'iglu:com.pathfactory/user_context/jsonschema/1-0-0',
data: {
  userid: 1234,
  name: 'john doe'
}
};

// if a global context has a conditional component (in this case, a schema rule set)
// our global context must be supplied as an array, where the first element is
// the conditional part and the context is the second element
let global_context = [
{accept: 'iglu:com.pathfactory/*/jsonschema/*'},
user_context
]

window.snowplow('addGlobalContexts', [global_context]);

I couldn’t see any thing in the tracker with snowplow plugin that data generated. infact it is not hitting the sp.js createglobalcontext method.

Could you please guide me how to enable this?

Thanks
Sudhir

This looks correct though the addGlobalContexts method itself doesn’t fire an event - you’ll need something like a trackPageView. In this case you have a condition for the global context so you’ll require a trackSelfDescribingJson that fires a com.pathfactory/*/jsonschema/* event to attach the global contexts.

Hi Mike,

Really I couldn’t enable this global context. Can you guide with some example. I have gone through with https://snowplowanalytics.com/blog/2019/01/23/snowplow-javascript-tracker-2.10.0-released-with-global-contexts/

JavaScriptTracker

let user_context = {
schema: ‘iglu:com.pathfactory/user_context/jsonschema/1-0-0’,
data: {
userid: 1234,
name: ‘john doe’
}
};

// if a global context has a conditional component (in this case, a schema rule set)
// our global context must be supplied as an array, where the first element is
// the conditional part and the context is the second element
let global_context = [
{accept: ‘iglu:com.pathfactory//jsonschema/’},
user_context
]

window.snowplow(‘addGlobalContexts’, [global_context]);

IgluCentral

https://s3.amazonaws.com/com.pathfactory/schemas/com.pathfactory/user_context/jsonschema/1-0-0

{
“$schema”: “http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#”,
“description”: “A Self describing JSON Schema for use with Iglu”,
“self”: {
“vendor”: “com.pathfactory”,
“name”: “user_context”,
“format”: “jsonschema”,
“version”: “1-0-0”
},

"type": "object",

"properties": {

	"userid": {
		"type": "number"			
	},
	"name": {
		"type": "string"
	}
},

"required": ["userid"],
"additionalProperties": false

}

What event are you trying to figure with the global context? You’ve attached a global context but there’s no Javascript that then fires any events for the global contexts to get attached to.

Hi Mike,

Thanks for your response. On behalf of @Sudhir_Batchu please find our tracker code below :

//Custom Context
window.snowplow('trackPageView', null, [
    {
        schema: "iglu:com.acme/page/jsonschema/1-2-1",
        data: {
            pageType: 'test',
            lastUpdated: new Date()
        }
	},
	{
		schema: "iglu:com.acme/user/jsonschema/2-0-0",
		data: {
			userType: 'tester'
		}
	}
]);

//trackSelfDescribingEvent
window.snowplow('trackSelfDescribingEvent', {
    schema: 'iglu:com.acme/viewed_product/jsonschema/2-0-0',
    data: {
        productId: 'Sen775',
        category: 'Electronics',
        brand: 'Apple',
        returning: true,
        price: 49.95
    }
});

// Enable Global Context
let user_context = {
  schema: 'iglu:com.acme/user_context/jsonschema/1-0-0',
  data: {
    userid: 1234,
    name: 'john doe'
  }
};

let global_context = [
  {accept: 'iglu:com.acme/*/jsonschema/*'},
  user_context
]
window.snowplow('addGlobalContexts',[global_context]);

Please let us know if we are missing anything.

We have tried setting the context as per below and it worked. Thanks.

window.snowplow('addGlobalContexts', [{
    schema: 'iglu:com.acme/user_context/jsonschema/1-0-0',
    data: {
        userid: 1234,
        name: 'john doe'
    }
}])

It would have been great if we had an example for Global Contexts in the wiki

1 Like

Thanks for the feedback on the docs. I agree with you, some more examples of how Global Contexts can be used would definitely be helpful in there.
I’ll look to improve this area of the docs with the next release of the JS Tracker.
Also, we have a new docs site now that is often more up to date than our GitHub wiki (which we’re in the process of migrating away from). Check it out here: https://docs.snowplowanalytics.com/

1 Like

Thanks @PaulBoocock. We’ll check it out.

You need to add global contexts before you send out an event.
Your code should look like this:

// Enable Global Context
let user_context = {
  schema: 'iglu:com.acme/user_context/jsonschema/1-0-0',
  data: {
    userid: 1234,
    name: 'john doe'
  }
};

let global_context = [
  {accept: 'iglu:com.acme/*/jsonschema/*'},
  user_context
]
window.snowplow('addGlobalContexts',[global_context]);
//Custom Context
window.snowplow('trackPageView', null, [
    {
        schema: "iglu:com.acme/page/jsonschema/1-2-1",
        data: {
            pageType: 'test',
            lastUpdated: new Date()
        }
	},
	{
		schema: "iglu:com.acme/user/jsonschema/2-0-0",
		data: {
			userType: 'tester'
		}
	}
]);

//trackSelfDescribingEvent
window.snowplow('trackSelfDescribingEvent', {
    schema: 'iglu:com.acme/viewed_product/jsonschema/2-0-0',
    data: {
        productId: 'Sen775',
        category: 'Electronics',
        brand: 'Apple',
        returning: true,
        price: 49.95
    }
});
1 Like

Thanks @mbondarenko.