Custom Javascript enrichment uses cookie info?

Hi Snowplowers,
We are using the awesome custom js enrichment and trying to get some information from Cookie and parse it.
However the methods that found available for Enrichedevnet are only:

  1. getUnstruct_event
  2. getPlatform
  3. getApp_id
    But did not see anything about Cookie.
    Would you please share any documentations of all methods and fields that are available for Javascript Enrichment? Thank you

Hey @kuangmichael07

You can find the JS Enrichment docs here: Custom JavaScript enrichment - Snowplow Docs

The full definition of the event that is available is here: https://github.com/snowplow/enrich/blob/81d108152b54961867cd1f6218b22465afb5c083/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/outputs/EnrichedEvent.scala

Any field in this object, is available with a get method. e.g. event.getUnstruct_event()

However, you won’t find cookies here by default, except for the properties which have already been populated from the Snowplow Cookies (such as network_userid, and fields beginning domain_*).

If you want to extract your own cookies before processing them you’ll need to use: Cookie extractor enrichment - Snowplow Docs

Once you’ve extracted them, they will be available in the JS Enrichment as context:

    var contexts = JSON.parse(event.getContexts());
    var contextArray = contexts.data;
1 Like

Thank you for the reply @PaulBoocock !
I tried your proposal and is able to extract the cookie, I am able to see the extracted cookie info in contexts_org_ietf_http_cookie_1 (name and value pairs). However

these two lines of code does not provide me the cookie info from contexts_org_ietf_http_cookie_1.

And all info that I can retrieve from contexts are:
{"schema":"iglu:com.google.analytics/cookies/jsonschema/1-0-0","data":{}},{"schema":"iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0","data":{"id":"cd8566e4-5dc1-490d-9efb-5cc9d995d55c"}},{"schema":"iglu:org.w3/PerformanceTiming/jsonschema/1-0-0","data":{"navigationStart":1631070490216,"unloadEventStart":1631070490230,"unloadEventEnd":1631070490230,"redirectStart":0,"redirectEnd":0,"fetchStart":1631070490217,"domainLookupStart":1631070490217,"domainLookupEnd":1631070490217,"connectStart":1631070490217,"connectEnd":1631070490217,"secureConnectionStart":0,"requestStart":1631070490222,"responseStart":1631070490224,"responseEnd":1631070490225,"domLoading":1631070490236,"domInteractive":1631070493432,"domContentLoadedEventStart":1631070493432,"domContentLoadedEventEnd":1631070493432,"domComplete":0,"loadEventStart":0,"loadEventEnd":0}},{"schema":"iglu:org.ietf/http_client_hints/jsonschema/1-0-0","data":{"isMobile":false,"brands":[{"brand":"Google Chrome","version":"93"},{"brand":" Not;A Brand","version":"99"},{"brand":"Chromium","version":"93"}]}}

Would you please shed some light? Thank you

BTW also I tried getDerived_contexts() method, and nothing returns from this method

Hmm - I’m not 100% sure if this is supported or not.

The difficulty here is that these enrichments won’t necessarily run in order - so the Javascript enrichment may be running before the HTTP cookie enrichment - which would explain why you are able to access contexts but not necessarily derived contexts that are added during the enrichment stage.

The easiest way around this is to add the cookie at collection time (if possible) and then it’ll be available for processing by the Javascript enrichment. There’s a ticket to support this sort of behaviour in the future but I’m not sure how far along it is (as it’s quite a complex one).

Thank you @mike
In that case if I want to parse a key value pair in the Cookie, which step should I try?
Would it be possible to do it in GTM? Or postprocess in Looker is the only way?
Thank you

Hi @kuangmichael07
My apologies, I was wrong that the cookie context from the cookie enrichment would be available in the JS Enrichment. The extra context information from the “context” generating enrichments is added after the JS Enrichment runs. Apologies for the confusion!

I think your best option is to parse it client side (in GTM) and add it as a context there (maybe even a global context, so its on all events). Using the cookie extractor enrichment and parsing it in Looker is also a reasonable idea although I think has the potential to slow down your query execution if the cookie is complicated.

1 Like

@PaulBoocock
Thanks again for the reply.
I tried addGlobalContext for all event.
(Note: we are logging the ab testing experiments/variants id)

And I successfully received a single global context by adding js code in GTM below:

function() {
  var cookieInfo = function () {
    var value = "; ${document.cookie}";
    var parts = value.split("; key=");
    if (parts.length === 2) return parts.pop().split(';').shift();
  }
  
  var abTesting = [
  {
    schema: 'iglu:com.test/abtesting/jsonschema/1-0-0',
    data: {
      abtesting_id: "testing",
      experiment_id: '1',
      variant_id: 'a'
    }
  },
  }];
  
 return abTesting;
}

However, since the requirement wants us to run more than one experiments at a time. What I have tried so far was:

function() {
  var cookieInfo = function () {
    var value = "; ${document.cookie}";
    var parts = value.split("; key=");
    if (parts.length === 2) return parts.pop().split(';').shift();
  }
  
  var abTesting = [
  {
    schema: 'iglu:com.test/abtesting/jsonschema/1-0-0',
    data: {
      abtesting_id: "testing",
      experiment_id: '1',
      variant_id: 'a'
    }
  },
  {
    schema: 'iglu:com.test/abtesting/jsonschema/1-0-0',
    data: {
      abtesting_id: "testing-2",
      experiment_id: '2',
      variant_id: 'b'
    }
  },
  }];
  
 return abTesting;
}

But this does not receive both of these contexts together in BQ, not like the other custom contexts that attached to custom events as an array.
Would you please shed some light again on this?
Thank you

NVM @PaulBoocock and @mike , I think I made it right this time, thank you so much for the help

1 Like