Skip to content

Commit

Permalink
Add Event Specification option map to Snowplow Media Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
igneel64 committed Mar 20, 2024
1 parent 929ac5d commit 02da135
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
24 changes: 19 additions & 5 deletions plugins/browser-plugin-media/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,30 @@ import {
MediaTrackErrorArguments,
MediaTrackSelfDescribingEventArguments,
EventWithContext,
MediaPluginOptions,
} from './types';
import { setEventSpecificationContext } from './utils';

export { MediaAdBreakType as MediaPlayerAdBreakType };

const _trackers: Record<string, BrowserTracker> = {};
const _context: Record<string, SelfDescribingJson[]> = {};
const defaultPluginOptions = {};

/**
* Adds media tracking
*/
export function SnowplowMediaPlugin(): BrowserPlugin {
export function SnowplowMediaPlugin(pluginOptions: MediaPluginOptions = defaultPluginOptions): BrowserPlugin {
let trackerId: string;
return {
activateBrowserPlugin: (tracker) => {
trackerId = tracker.id;
_trackers[trackerId] = tracker;
_context[trackerId] = [];
},
beforeTrack(payloadBuilder) {
setEventSpecificationContext(payloadBuilder, pluginOptions.eventSpecificationIds);
},
contexts: () => {
return _context[trackerId] || [];
},
Expand Down Expand Up @@ -396,7 +402,9 @@ export function trackMediaAdSkip(
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
if (percentProgress !== undefined) {
percentProgress = Math.floor(percentProgress);
}
track(
{
mediaEvent: {
Expand Down Expand Up @@ -506,7 +514,9 @@ export function trackMediaAdClick(
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
if (percentProgress !== undefined) {
percentProgress = Math.floor(percentProgress);
}
track(
{
mediaEvent: {
Expand Down Expand Up @@ -534,7 +544,9 @@ export function trackMediaAdPause(
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
if (percentProgress !== undefined) {
percentProgress = Math.floor(percentProgress);
}
track(
{
mediaEvent: {
Expand Down Expand Up @@ -563,7 +575,9 @@ export function trackMediaAdResume(
trackers: Array<string> = Object.keys(_trackers)
) {
let { percentProgress } = args;
if (percentProgress !== undefined) { percentProgress = Math.floor(percentProgress); }
if (percentProgress !== undefined) {
percentProgress = Math.floor(percentProgress);
}
track(
{
mediaEvent: {
Expand Down
7 changes: 7 additions & 0 deletions plugins/browser-plugin-media/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { CommonEventProperties, SelfDescribingJson } from '@snowplow/tracker-core';

export interface MediaPluginOptions {
/**
* Option to match Media Events to Event Specification identifiers. Keys are almost always MediaEventType + "_event"
*/
eventSpecificationIds?: Record<string, string>;
}

/** Type of media player event */
export enum MediaEventType {
// Controlling the playback
Expand Down
42 changes: 42 additions & 0 deletions plugins/browser-plugin-media/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PayloadBuilder } from '@snowplow/tracker-core';

function getEventSchemaName(payloadBuilder: PayloadBuilder): string | undefined {
const payloadJson = payloadBuilder.getJson();
const mediaEventSchema = payloadJson.find(
(e) =>
e.keyIfEncoded === 'ue_px' &&
(e.json.data as { schema: string }).schema.match(/iglu:com.snowplowanalytics.snowplow.media\/.*\/jsonschema/)
);
if (typeof mediaEventSchema === 'undefined') {
return;
}
// We know schemas are in this otherwise it would not match the above regex.
const eventSourceName = (mediaEventSchema.json.data as { schema: string }).schema.match(
/iglu:com.snowplowanalytics.snowplow.media\/(.*)\/jsonschema/
)![1];

return eventSourceName;
}

function createEventSpecificationContext(eventSpecificationId: string) {
return {
schema: 'iglu:com.snowplowanalytics.snowplow/event_specification/jsonschema/1-0-0',
data: {
id: eventSpecificationId,
},
};
}

export function setEventSpecificationContext(
payloadBuilder: PayloadBuilder,
eventSpecificationIds?: Record<string, string>
) {
if (!eventSpecificationIds) {
return;
}
const eventName = getEventSchemaName(payloadBuilder);
const eventSpecificationId = eventName && eventSpecificationIds[eventName];
if (eventSpecificationId) {
payloadBuilder.addContextEntity(createEventSpecificationContext(eventSpecificationId));
}
}

0 comments on commit 02da135

Please sign in to comment.