Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions dist/videojs.ga.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* videojs-ga - v0.4.2 - 2015-02-06
* Copyright (c) 2015 Michael Bensoussan
* videojs-ga - v0.4.2 - 2016-02-10
* Copyright (c) 2016 Michael Bensoussan
* Licensed MIT
*/
(function() {
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

videojs.plugin('ga', function(options) {
var dataSetupOptions, defaultsEventsToTrack, end, error, eventCategory, eventLabel, eventsToTrack, fullscreen, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, resize, seekEnd, seekStart, seeking, sendbeacon, timeupdate, volumeChange;
var dataSetupOptions, defaultsEventsToTrack, disabletracking, enabletracking, end, error, eventCategory, eventLabel, eventsToTrack, fullscreen, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, resize, seekEnd, seekStart, seeking, sendbeacon, setoptions, timeupdate, tracking, volumeChange;
if (options == null) {
options = {};
}
Expand All @@ -19,14 +19,22 @@
}
}
defaultsEventsToTrack = ['loaded', 'percentsPlayed', 'start', 'end', 'seek', 'play', 'pause', 'resize', 'volumeChange', 'error', 'fullscreen'];
eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack;
percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10;
eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video';
eventLabel = options.eventLabel || dataSetupOptions.eventLabel;
options.debug = options.debug || false;
eventsToTrack = null;
percentsPlayedInterval = null;
eventCategory = null;
eventLabel = null;
percentsAlreadyTracked = [];
seekStart = seekEnd = 0;
seeking = false;
tracking = true;
setoptions = function(newoptions) {
options = newoptions;
eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack;
percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10;
eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video';
eventLabel = options.eventLabel || dataSetupOptions.eventLabel;
return options.debug = options.debug || false;
};
loaded = function() {
if (!eventLabel) {
eventLabel = this.currentSrc().split("/").slice(-1)[0].replace(/\.(\w{3,4})(\?.*)?$/i, '');
Expand Down Expand Up @@ -102,6 +110,9 @@
}
};
sendbeacon = function(action, nonInteraction, value) {
if (!tracking) {
return;
}
if (window.ga) {
ga('send', 'event', {
'eventCategory': eventCategory,
Expand All @@ -116,6 +127,12 @@
console.log("Google Analytics not detected");
}
};
disabletracking = function() {
return tracking = false;
};
enabletracking = function() {
return tracking = true;
};
this.ready(function() {
this.on("loadedmetadata", loaded);
this.on("timeupdate", timeupdate);
Expand All @@ -141,8 +158,12 @@
return this.on("fullscreenchange", fullscreen);
}
});
setoptions(options);
return {
'sendbeacon': sendbeacon
'sendbeacon': sendbeacon,
'setoptions': setoptions,
'enabletracking': enabletracking,
'disabletracking': disabletracking
};
});

Expand Down
6 changes: 3 additions & 3 deletions dist/videojs.ga.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 38 additions & 13 deletions src/videojs.ga.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,29 @@ videojs.plugin 'ga', (options = {}) ->
'end', 'seek', 'play', 'pause', 'resize',
'volumeChange', 'error', 'fullscreen'
]
eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack
percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10

eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video'
# if you didn't specify a name, it will be 'guessed' from the video src after metadatas are loaded
eventLabel = options.eventLabel || dataSetupOptions.eventLabel

# if debug isn't specified
options.debug = options.debug || false
eventsToTrack = null
percentsPlayedInterval = null
eventCategory = null
eventLabel = null

# init a few variables
percentsAlreadyTracked = []
seekStart = seekEnd = 0
seeking = false
tracking = true

setoptions = (newoptions) ->
options = newoptions
eventsToTrack = options.eventsToTrack || dataSetupOptions.eventsToTrack || defaultsEventsToTrack
percentsPlayedInterval = options.percentsPlayedInterval || dataSetupOptions.percentsPlayedInterval || 10

eventCategory = options.eventCategory || dataSetupOptions.eventCategory || 'Video'
# if you didn't specify a name, it will be 'guessed' from the video src after metadatas are loaded
eventLabel = options.eventLabel || dataSetupOptions.eventLabel

# if debug isn't specified
options.debug = options.debug || false

loaded = ->
unless eventLabel
Expand Down Expand Up @@ -111,20 +120,29 @@ videojs.plugin 'ga', (options = {}) ->
return

sendbeacon = ( action, nonInteraction, value ) ->
unless tracking
return

# console.log action, " ", nonInteraction, " ", value
if window.ga
ga 'send', 'event',
'eventCategory' : eventCategory
'eventAction' : action
'eventLabel' : eventLabel
'eventCategory' : eventCategory
'eventAction' : action
'eventLabel' : eventLabel
'eventValue' : value
'nonInteraction' : nonInteraction
'nonInteraction' : nonInteraction
else if window._gaq
_gaq.push(['_trackEvent', eventCategory, action, eventLabel, value, nonInteraction])
else if options.debug
console.log("Google Analytics not detected")
return

disabletracking = ->
tracking = false

enabletracking = ->
tracking = true

@ready ->
@on("loadedmetadata", loaded)
@on("timeupdate", timeupdate)
Expand All @@ -136,4 +154,11 @@ videojs.plugin 'ga', (options = {}) ->
@on("error", error) if "error" in eventsToTrack
@on("fullscreenchange", fullscreen) if "fullscreen" in eventsToTrack

return 'sendbeacon': sendbeacon
setoptions(options)

return {
'sendbeacon': sendbeacon
'setoptions': setoptions
'enabletracking': enabletracking
'disabletracking': disabletracking
}