Adding custom contexts to self describing events

Hello! We are trying to track an add-to-cart event in our React Native (Expo Bare Workflow) application and add the product details as a custom product context, rather than as event properties so that they can be reused for other e-commerce events.

The React Native documentation doesn’t include an example of how to attach contexts to self-describing events, so we’re a little confused about how to structure it. Here’s what we have:

tracker.trackSelfDescribingEvent(
    {
      schema:
        "iglu:com.snowplowanalytics.snowplow/add_to_cart/jsonschema/1-0-0",
      data: {},
    },
    { contexts: [productContext] }, // <--- what does this look like?
  );
};

The productContext is an EventContext type that was generated by a helper method

But we keep getting this error:
Argument of type ‘{ contexts: EventContext[]; }’ is not assignable to parameter of type ‘EventContext[]’.

  • Object literal may only specify known properties, and ‘contexts’ does not exist in type ‘EventContext’.ts(2345)*

This seems like it should be straightforward so appreciate any help!

Hey @jkreiz ,

You can find some examples in the React Native tracker docs here (they only show how to add entities to trackScreenViewEvent() call but it is the same with trackSelfDescribingEvent()): Tracking events | Snowplow Documentation

Instead of the {context: [entity]} object, you should just track the [entity] array as the second parameter of the track function. So something like:


tracker.trackSelfDescribingEvent(
    {
      schema:
        "iglu:com.snowplowanalytics.snowplow/add_to_cart/jsonschema/1-0-0",
      data: {},
    },
    [productContext]
  );
};
2 Likes

Hi @jkreiz, contexts argument has an array of self-describing JSONs as its value. Your own code you presented seems to be “faulty” as well. Depending on the tracker and its version it could have different representation. When it comes to React Native, the custom self-describing event with the contexts could look like this

tracker.trackSelfDescribingEvent(
    {
      schema:
        "iglu:com.snowplowanalytics.snowplow/add_to_cart/jsonschema/1-0-0",
      data: {},
    },
    [
      {
        schema: 'iglu:com.example/user/jsonschema/2-0-0',
        data: {}
      },
      // another entity
    ]
);

Thank you both! This resolved the error :tada: but now events sent with contexts are not flowing into our BigQuery events table despite conforming to the schema. We tried adding a product context to our trackScreenViewEvent(), which was working previously, and it stopped logging. We are using the product context in Iglu Central.

Is there a reason adding a context would prevent the event from appearing in BQ?

Are you sure that the events are not ending up in the “bad event” queue due to validation errors?

Yes! Looks like they were. Thank you :slight_smile: Not sure if this is what we should have been doing, but we were using http://iglucentral.com/ to view schemas, which does not perfectly match the GitHub schema repository. There were required fields in our context schema that were only viewable in GitHub – there was no indication in Iglu Central that the schema we were referencing had required fields.

Hey @jkreiz, could you provide an example of the mismatch, please? I find it odd that there would be any.

Sure! the add_to_cart schema (iglu:com.snowplowanalytics.snowplow/add_to_cart/jsonschema/1-0-0), for example, does not show required fields in Iglu Central (screenshot), but in GitHub it does

Thanks @jkreiz , we have raised an internal ticket to have it resolved. Indeed, for now it’s best to rely on the schemas on GitHub.