Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

GA commands now prefix their command name with the name of the GA tracker #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ If set, this tracker code will be used for iframe embeds and the direct player U

**default:** Not set

####trackerName

If set, the tracker with this name will be used for in-page embeds. This should match the ```name``` field sent in the GA ```create``` command for the tracker you want associated with the player. If you don't know what it is please check [GA's doc](https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers)

**default:** Not set

####eventNames

Override or localise the names of the event actions.
Expand Down
37 changes: 32 additions & 5 deletions dist/videojs.ga.videocloud.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* videojs-ga - v0.4.1 - 2015-08-10
* Copyright (c) 2015 Michael Bensoussan
* videojs-ga - v0.4.2 - 2016-11-22
* 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 adStateRegex, currentVideo, dataSetupOptions, defaultLabel, defaultsEventsToTrack, end, endTracked, error, eventCategory, eventLabel, eventNames, eventsToTrack, fullscreen, getEventName, href, iframe, isInAdState, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, player, referrer, resize, seekEnd, seekStart, seeking, sendbeacon, sendbeaconOverride, start, startTracked, timeupdate, tracker, volumeChange,
var adStateRegex, currentVideo, dataSetupOptions, defaultLabel, defaultsEventsToTrack, end, endTracked, error, eventCategory, eventLabel, eventNames, eventsToTrack, fullscreen, getCommandName, getEventName, getTrackerName, href, iframe, isInAdState, loaded, parsedOptions, pause, percentsAlreadyTracked, percentsPlayedInterval, play, player, referrer, resize, seekEnd, seekStart, seeking, sendbeacon, sendbeaconOverride, start, startTracked, timeupdate, tracker, volumeChange,
_this = this;
if (options == null) {
options = {};
Expand Down Expand Up @@ -67,6 +67,33 @@
}
return name;
};
getCommandName = function(name) {
var trackerName;
if (!window.ga || name === 'create') {
return name;
}
trackerName = getTrackerName();
if (trackerName) {
return trackerName + '.' + name;
}
return name;
};
getTrackerName = function() {
var trackers;
if (options.trackerName) {
return options.trackerName;
}
if (dataSetupOptions.trackerName) {
return dataSetupOptions.trackerName;
}
if (window.ga && window.ga.getAll) {
trackers = ga.getAll();
if (trackers.length > 0) {
return trackers[0].get('name');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if you know - why would there be more than one tracker and should we fire events to all trackers?

}
}
return null;
};
if (window.location.host === 'players.brightcove.net' || window.location.host === 'preview-players.brightcove.net') {
tracker = options.tracker || dataSetupOptions.tracker;
if (tracker) {
Expand Down Expand Up @@ -199,7 +226,7 @@
if (sendbeaconOverride) {
sendbeaconOverride(eventCategory, action, eventLabel, value, nonInteraction);
} else if (window.ga) {
ga('send', 'event', {
ga(getCommandName('send'), 'event', {
'eventCategory': eventCategory,
'eventAction': action,
'eventLabel': eventLabel,
Expand All @@ -224,7 +251,7 @@
if (sendbeaconOverride) {
sendbeaconOverride(eventCategory, getEventName('player_load'), href, iframe, true);
} else if (window.ga) {
ga('send', 'event', {
ga(getCommandName('send'), 'event', {
'eventCategory': eventCategory,
'eventAction': getEventName('player_load'),
'eventLabel': href,
Expand Down
6 changes: 3 additions & 3 deletions dist/videojs.ga.videocloud.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "videojs-ga",
"version": "0.4.1",
"version": "0.4.2",
"license": "MIT",
"author": {
"name": "Michael Bensoussan",
Expand Down
27 changes: 25 additions & 2 deletions src/videojs.ga.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ videojs.plugin 'ga', (options = {}) ->
return eventNames[name]
return name

getCommandName = ( name ) ->
if !window.ga || name == 'create'
return name

trackerName = getTrackerName()
if trackerName
return trackerName + '.' + name

return name

getTrackerName = () ->
if options.trackerName
return options.trackerName
if dataSetupOptions.trackerName
return dataSetupOptions.trackerName

if window.ga && window.ga.getAll
trackers = ga.getAll()
if trackers.length > 0
return trackers[0].get('name')

return null

# load ga script if in iframe and tracker option is set
if window.location.host == 'players.brightcove.net' || window.location.host == 'preview-players.brightcove.net'
tracker = options.tracker || dataSetupOptions.tracker
Expand Down Expand Up @@ -203,7 +226,7 @@ videojs.plugin 'ga', (options = {}) ->
if sendbeaconOverride
sendbeaconOverride(eventCategory, action, eventLabel, value, nonInteraction)
else if window.ga
ga 'send', 'event',
ga getCommandName('send'), 'event',
'eventCategory' : eventCategory
'eventAction' : action
'eventLabel' : eventLabel
Expand All @@ -226,7 +249,7 @@ videojs.plugin 'ga', (options = {}) ->
if sendbeaconOverride
sendbeaconOverride(eventCategory, getEventName('player_load'), href, iframe, true)
else if window.ga
ga 'send', 'event',
ga getCommandName('send'), 'event',
'eventCategory' : eventCategory
'eventAction' : getEventName('player_load')
'eventLabel' : href
Expand Down