Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Last active May 28, 2020 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/c400378d5e10f1b409659dd54ce3d34b to your computer and use it in GitHub Desktop.
Save tzkmx/c400378d5e10f1b409659dd54ce3d34b to your computer and use it in GitHub Desktop.
Monitoreo reproducción video a los 5 segundos (Google Tag Manager, play event at 5 seconds started)

Real event analytics is fired by platform tag (Universal Analytics)

  • uses custom Data Layer Variables set by monitor script
  • uses custom event, verifies category is set to value by monitor
  • only invoked once

LICENSE: WTFPL

/**
* Uses custom regexp for activation on pages
* returns false if video element is already played (data-played="true")
*/
function () {
var isVideoPage = /^\/(embed|share-video)\/[0-9]+$/.test(location.pathname)
var video = document.getElementById('shared-video')
var isReady = video && (! Boolean(video.dataset['played']))
return isVideoPage && isReady
}
/**
* Tag, set event target, abort early if listener already added
* activated by custom variable.
* @see isPlayerReady.js
*
*/
;(function() {
var video = document.getElementById('shared-video')
if (video.dataset['listener']) return
var alreadyReported = false
video.dataset['played'] = alreadyReported
video.dataset['listener'] = 'set'
var playedVideoMark = 5
function eventHandler(e) {
location.hash === '#debug' && console.log({type: e.type, reported: alreadyReported, played: video.dataset['played']})
if (e.type !== 'timeupdate') return
if (alreadyReported) {
video.dataset['played'] = true
video.removeEventListener("timeupdate", eventHandler, false)
return
}
if (e.target.currentTime > playedVideoMark) {
alreadyReported = true
var hasVideoId = /[0-9]+/.exec(location.pathname)
var label = (hasVideoId === null ? '' : hasVideoId[0]) +
':' + decodeURIComponent(document.title)
dataLayer.push({
'event': 'event',
'gaEventCategory': 'Shared Video Played',
'gaEventAction': 'player.reached.' + playedVideoMark.toString(),
'gaEventLabel': label
})
}
}
// configure the listener
video.addEventListener("timeupdate", eventHandler, false)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment