Adding some additional data during Media tracking

Hello.
I track media in such a way:

import { newTracker } from '@snowplow/browser-tracker';
import { MediaTrackingPlugin, enableMediaTracking } from '@snowplow/browser-plugin-media-tracking';

newTracker('sp', 'https://my_collector', {
        appId: 'my_app_id',
        plugins: [MediaTrackingPlugin()],
    });

    enableMediaTracking({
        id: 'my_media_id',
        options: {
             captureEvents: ['play', 'pause', 'ended'],
        }
    });

And when, for instance, I pause a video I’ll receive "type": "pause" in my event.
But I want to add some extra data (e. g. customData) which has to be passed also incide each event.
So as a result, I want each event to contains:

"type": "pause/play/ended"
"customData": "someValue"

Which way can I add this extra data?
Could you help me with that please.

Hi @Axolotl

Take a look at the Global Context functionality. This lets you add entities to all events or a subset of event types: Tracking Events | Snowplow Documentation

If you just want it for media events, then you’ll likely want to create a context generator function as described in the docs.

2 Likes

Thank you @PaulBoocock

I try to add extra data in such a way:

import { newTracker, addGlobalContexts } from '@snowplow/browser-tracker';
import { MediaTrackingPlugin, enableMediaTracking } from '@snowplow/browser-plugin-media-tracking';

let contextEntity = {
        schema: 'iglu:com.acme/user_context/jsonschema/1-0-0',
        data: { userid: 1234, name: 'John Doe' }
    };

addGlobalContexts([contextEntity]);

newTracker('sp', 'https://my_collector', {
        appId: 'my_app_id',
        plugins: [MediaTrackingPlugin()],
    });

enableMediaTracking({
        id: 'my_media_id',
        options: {
             captureEvents: ['play', 'pause', 'ended'],
        }
    });

But I don’t receive any extra data like userid and name
Should I create a schema for this?

Hi @Axolotl ,

Yes you’ll need a schema. Here’s an overview of the topic, to help give you context: Structuring your data with schemas | Snowplow Documentation

Global contexts are a powerful feature, but if you haven’t used custom contexts before, debugging might be difficult. If you run into issues, I suggest following the documentation on attaching manual contexts - the page view example is a nice one to begin with.

Then, once you’ve done that successfully, it will be easier to debug global contexts. :slight_smile:

1 Like

Thank you @Colm

So in case of custom context, my code should be:

import { newTracker } from '@snowplow/browser-tracker';
import { MediaTrackingPlugin, enableMediaTracking } from '@snowplow/browser-plugin-media-tracking';

newTracker('sp', 'https://my_collector', {
        appId: 'my_app_id',
        plugins: [MediaTrackingPlugin()],
    });

enableMediaTracking({
        id: 'my_media_id',
        options: {
             captureEvents: ['play', 'pause', 'ended'],
        },
        context: [{
            schema: "iglu:com.example_company/user/jsonschema/2-0-0",
            data: {
                userType: 'tester'
            }
        }]
    });

Is it correct?

There is a difference between the enable... functions - which configure the tracker options, and the track... functions, which manually track an event.

All track... functions can take a context argument, but the enable... functions can’t (Some of them might be able to, I’m not sure - it’s been a while since I’ve worked on trackers!).

In this instance, enableMediaTracking switches on the configuration for the tracker to automatically capture media tracking events. It can’t take a context argument the way you have configured.

However, the global context feature that Paul suggested can add contexts to those automatically tracked events.

My suggestion is to experiment with a track... method first, to get used to how contexts work, and then use the global contexts feature to add contexts to the media events.

It might help if I explain why a bit more explicitly:

In general, using custom contexts (and custom events) requires you to:

  • Have an Iglu Server set up to host schemas
  • Create a schema for the custom data and upload it to the Iglu Server
  • Set up your tracking to refer to that schema

The enrich component then uses the schema to validate the data. This in itself isn’t terribly complicated once you understand how it works - but it’s best to understand this part well as a first step, because debugging is much easier when you’re familiar with it. We typically recommend to use Snowplow Mini, or Snowplow Micro to help with the debugging of schemas and tracking.

I think it’ll be easiest to experiment with this using a simple trackPageView first - then once you’ve done so, it will be much easier to implement global contexts.

Of course you can go straight to implementing global contexts if you wish, it just might be harder to debug, since you have the complexity of the global context api to deal with on top of the above. (Which is also not hugely complex on its own, but it can get very confusing if you haven’t got to grips with the above concepts first).

I hope that’s helpful!

1 Like

Thank you so much @Colm for so a comprehensive explanation!
I’ll try to!

1 Like