Method for getting existing global contexts

Hey there.

We are using Snowplow in our organisation for tracking various events. We are also utilising global context functionality. However, we are running into an issue when we attach global contexts that already exist on the global scope. While checking the documentation we didn’t find any methods apart from addGlobalContexts, removeGlobalContexts, clearGlobalContexts. Could you please advise on how we can only addGlobalContexts when there is no such globalContext just yet? In other words, check if the context already exists and if not, add it (something like getGlobalContexts method). We haven’t found any methods to do that in your documentation.

This is messing up our analytics a bit, so any ideas are appreciated. Thanks!

Hi @justbigmack ,

There isn’t such a method at present, but I think it’s a good suggestion.

If your use case happens to not care about which context is attached (first or second), and the main pain is having duplicate contexts, then simply calling remove before add will ensure that only the second one is sent.

If you have a requirement that the context first attached to global contexts is kept, then I think this may be a limitation of the trackers at present, I’m not sure how to achieve it in the tracker unfortunately. I’ve raised it with the trackers team, to figure out if we should add something like you suggest to the feature.

I can only think of one workaround at the moment - if you include a timestamp in the contexts (using a context generator to do so), then you could either use SQL to select just the first one in your model, or use the Javascript enrichment to pull just the first one into a derived context, and use that in your model.

Both are awkward workarounds, I’d probably say using SQL is the least awkward.

Sorry not to have a happier answer for this, like I said I’ve raised it with the relevant team so if they have a better workaround they will chime in!


Hi @justbigmack
Welcome to the Snowplow Community.

Hi @justbigmack ,

sorry if I’m replying with more questions. It’s only to frame a bit better the problem you are facing.
What tracker do you use? I guess it’s a mobile tracker from the methods you mentioned.
Which version are you using?

With the last version 2.x of the mobile trackers you should be able to get the list of Global Context generators you added from the method getTags(). It provides the list of label associated to the Global Context generators you added.

If you use a previous version the API can be slightly different.

Please, let me know if I haven’t understood correctly the issue.

1 Like

Hey Colm! Thanks for your suggestions! We were thinking about it and were hoping there would be a built-in method. We will go with a workaround for now. Also, I appreciate you passing it to the relevant team!

Hey @Alex_Benini ! We are not using the mobile version of the tracker. We are using a Javascript tracker version 2.16.3.