-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga4PartnerPlayerEvents.js
executable file
·149 lines (129 loc) · 5.62 KB
/
ga4PartnerPlayerEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// v20230501
(function() {
var PBS_Partner_Players = [];
var getFrameTarget = function(evt) {
var frames = document.querySelector('body').getElementsByTagName('iframe')
, frameId = 0
, framesLength = frames.length;
for (; frameId < framesLength; frameId++) {
if (frames[frameId].contentWindow === evt.source) {
return {
"index": frameId,
"value": frames[frameId]
};
}
}
return null;
};
var getPartnerPlayerFromEvent = function(evt) {
var partnerPlayer = false;
if (PBS_Partner_Players) {
partnerPlayer = PBS_Partner_Players.find(function(partnerplayer) {
return partnerplayer.value == getFrameTarget(evt).value;
});
}
return partnerPlayer;
};
function determineEventType(action, value, evt) {
var currentVideoPlayer;
if (action === "video") {
// Get current iframe object from PBS_Partner_Players
currentVideoPlayer = getPartnerPlayerFromEvent(evt);
// Create object copy to prevent same object reference in dataLayer for different event
currentVideoPlayer.data = Object.assign({}, currentVideoPlayer.data);
if (currentVideoPlayer) {
// video_start
if (value === "playing" && currentVideoPlayer.firstView) {
currentVideoPlayer.firstView = false;
currentVideoPlayer.data.pbs_video_percent = 0;
currentVideoPlayer.data.pbs_video_action = 'start';
// Submit Event
// console.log('start', currentVideoPlayer.data);
submitPartnerPlayerEvent(currentVideoPlayer.data);
// video_complete
} else if (value === 'finished') {
currentVideoPlayer.data.pbs_video_percent = 100;
currentVideoPlayer.data.pbs_video_action = 'complete';
// Track Event
// console.log('complete', currentVideoPlayer.data);
submitPartnerPlayerEvent(currentVideoPlayer.data);
}
}
}
}
function submitPartnerPlayerEvent(vid_data){
window.dataLayer = window.dataLayer || [];
dataLayer.push(vid_data);
// console.log('pushing:', vid_data);
}
function videoMessageHandler(evt) {
// Only read messages coming from origin window: player.pbs.org
if (/^https?:\/\/player.pbs.org/.test(evt.origin)) {
var data = evt.data;
var hasSeparator = /::/.test(data);
var action = (hasSeparator) ? data.split('::')[0] : data;
var value = (hasSeparator) ? data.split('::')[1] : '';
var videoInfoRegex = /({"title":")(.*)(?=","airdate")/g;
var isVideoInfo = videoInfoRegex.test(data);
var frameTarget, vidData;
// console.log('isVideoInfo', isVideoInfo, '\nDATA:', data, '\nACTION:', action, '\nVALUE:', value);
// If message is video information/data, setup frame in PBS_Partner_Players
if (isVideoInfo) {
// Setup frame in PBS_Partner_Players
vidData = JSON.parse(value);
frameTarget = getFrameTarget(evt);
frameTarget.data = {
'event': 'partnerPlayerEvent',
'pbs_video_duration': vidData.duration,
'pbs_video_provider': 'PBS Partner Player',
'pbs_video_title': vidData.program.title + " - " + vidData.title,
'pbs_video_url': frameTarget.value.src,
'pbs_video_embed_url': window.location.href
// NOT ADDED: Info not available from message data
// ,'pbs_video_current_time': (result.event/100)*pbs_video_duration
// ,'pbs_video_percent': result.event, // this will be 0 for video_start, and 100 for video_complete (Added in determineEventType function)
// ,'visible': 1 // 1 or 0 if player visible on screen while video engagement was tracked
};
frameTarget.firstView = true;
PBS_Partner_Players.push(frameTarget);
}
// If message data (action::value) is video::playing or video::finished (ignored: video::intitialized)
// then process event type
if (action === 'video' && (value === 'playing' || value === 'finished')) {
determineEventType(action, value, evt);
}
}
}
function isPartnerPlayerOnPage() {
for (var e = document.getElementsByTagName("iframe"), x=0; x < e.length; x++) {
if (/^https?:\/\/player.pbs.org/.test(e[x].src)) {
return true;
}
}
return false;
}
function partnerPlayerEvents() {
var hasPartnerPlayer = isPartnerPlayerOnPage();
// If PBS Partner Player exists, setup addEventListener for all iframe messages
if (hasPartnerPlayer) {
if (typeof window.addEventListener === 'function') {
window.addEventListener('message', videoMessageHandler, false)
} else {
window.attachEvent('onmessage', videoMessageHandler)
}
}
}
// CheckDOM
(function(d, f) {
if (d.readyState === 'loading') {
d.addEventListener('readystatechange', function() {
if (d.readyState === 'interactive') {
f();
}
});
} else {
f();
}
})(document, partnerPlayerEvents);
}
)();