-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate to Media Tracking plugin for HTML5 Plugin (#1344)
- Loading branch information
Showing
29 changed files
with
1,127 additions
and
1,399 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...wser-plugin-media-tracking/migrate-html5-plugin-to-new-media-plugin_2024-09-24-12-28.json
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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@snowplow/browser-plugin-media-tracking", | ||
"comment": "Migrate HTML Media Tracking to Snowplow Media Plugin", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@snowplow/browser-plugin-media-tracking" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
{ | ||
"pnpmShrinkwrapHash": "c364f96ddb4f2ccb56f10dceb499162dffc440f8", | ||
"pnpmShrinkwrapHash": "64adeaeaaaae09f264464fa44834bfe036401311", | ||
"preferredVersionsHash": "bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f" | ||
} |
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,82 @@ | ||
import { Logger } from '@snowplow/tracker-core'; | ||
import { BrowserPlugin, BrowserTracker } from '@snowplow/browser-tracker-core'; | ||
import { waitForElement } from './findElem'; | ||
import { Config, isElementConfig, isStringConfig } from './config'; | ||
import { setUpListeners } from './player'; | ||
import { setConfigDefaults } from './helperFunctions'; | ||
|
||
// These imports are used for documentation purposes only. | ||
// Typescript complains that they are unused. | ||
// @ts-ignore: TS6133 | ||
import { DynamicContext } from '@snowplow/tracker-core'; | ||
// @ts-ignore: TS6133 | ||
import { HTML5MediaEventTypes } from './config'; | ||
// @ts-ignore: TS6133 | ||
import { FilterOutRepeatedEvents } from '@snowplow/browser-plugin-media/src/types'; | ||
|
||
import { endMediaTracking } from '@snowplow/browser-plugin-media'; | ||
|
||
let LOG: Logger; | ||
const _trackers: Record<string, BrowserTracker> = {}; | ||
|
||
export function MediaTrackingPlugin(): BrowserPlugin { | ||
return { | ||
activateBrowserPlugin: (tracker: BrowserTracker) => { | ||
_trackers[tracker.id] = tracker; | ||
}, | ||
logger: (logger) => { | ||
LOG = logger; | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Enables media tracking on an HTML `<video>` or `<audio>` element. | ||
* | ||
* @param {Config} config - Configuration options for media tracking. | ||
* | ||
* **Required:** | ||
* - `config.id` (`string`): The session ID. Must be unique for each media element. Used to identify the media session and end tracking. | ||
* - `config.video` (string | HTMLMediaElement): The ID of the media element or the element itself. | ||
* | ||
* **Optional:** | ||
* - `config.label` (`string`): A human-readable label for the media element. | ||
* - `config.captureEvents` {@link HTML5MediaEventTypes}: A list of media events to track. All events are tracked by default. | ||
* - `config.boundaries` (`number[]`): Percentage thresholds (0-100) at which to trigger progress events. Disabled by default. | ||
* - `config.context` {@link DynamicContext}: Contexts to attach to each tracking event. | ||
* | ||
* **Ping Configuration (Optional):** | ||
* - `config.pings.pingInterval` (`number`): The interval (in seconds) for sending ping events. Default is 30(s). | ||
* - `config.pings.maxPausedPings` (`number`): The maximum number of ping events sent while playback is paused. Default is 1. | ||
* | ||
* **Other Options (Optional):** | ||
* - `config.updatePageActivityWhilePlaying` (`boolean`): Whether to update page activity while media is playing. Enabled by default. | ||
* - `config.filterOutRepeatedEvents` {@link FilterOutRepeatedEvents}: Whether to suppress consecutive identical events. Default is false. | ||
*/ | ||
export function startHtml5MediaTracking(config: Config) { | ||
if (!config.video) { | ||
LOG.error( | ||
"Missing 'video' property in the enableMediaTracking configuration. ", | ||
"Ensure the 'config.video' property is correctly set. Current config: ", | ||
config | ||
); | ||
return; | ||
} | ||
|
||
config = setConfigDefaults(config); | ||
|
||
if (isStringConfig(config)) { | ||
waitForElement(config, setUpListeners); | ||
} else if (isElementConfig(config)) { | ||
setUpListeners(config); | ||
} | ||
} | ||
|
||
/** | ||
* Ends media tracking with the given ID if previously started. Clears local state for the media tracking and sends any events waiting to be sent. | ||
* | ||
* @param {string} id - The session ID. | ||
*/ | ||
export function endHtml5MediaTracking(id: string) { | ||
endMediaTracking({ id }); | ||
} |
94 changes: 0 additions & 94 deletions
94
plugins/browser-plugin-media-tracking/src/buildMediaEvent.ts
This file was deleted.
Oops, something went wrong.
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 { MediaEventType } from '@snowplow/browser-plugin-media'; | ||
import { CommonMediaEventProperties, MediaTrackingConfiguration } from '@snowplow/browser-plugin-media/src/types'; | ||
|
||
export type HTML5MediaEventTypes = Extract< | ||
MediaEventType, | ||
| MediaEventType.Ready | ||
| MediaEventType.Play | ||
| MediaEventType.Pause | ||
| MediaEventType.End | ||
| MediaEventType.SeekEnd | ||
| MediaEventType.PlaybackRateChange | ||
| MediaEventType.VolumeChange | ||
| MediaEventType.FullscreenChange | ||
| MediaEventType.PictureInPictureChange | ||
| MediaEventType.BufferStart | ||
| MediaEventType.BufferEnd | ||
| MediaEventType.Error | ||
| MediaEventType.Ping | ||
| MediaEventType.PercentProgress | ||
>; | ||
|
||
export type Config = { | ||
video: string | HTMLMediaElement; | ||
label?: string; | ||
} & CommonMediaEventProperties & | ||
Omit<MediaTrackingConfiguration, 'player'>; | ||
|
||
export interface StringConfig extends Config { | ||
video: string; | ||
} | ||
|
||
export function isStringConfig(config: Config): config is StringConfig { | ||
return typeof config.video === 'string'; | ||
} | ||
|
||
export interface ElementConfig extends Config { | ||
video: HTMLMediaElement; | ||
} | ||
|
||
export function isElementConfig(config: Config): config is ElementConfig { | ||
return config.video instanceof HTMLMediaElement; | ||
} |
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
Oops, something went wrong.