-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Event Specification option map to Snowplow Media Plugin
- Loading branch information
Showing
3 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |