Player example
This example uses a Brightcove player and adds Google Analytics programmatically, assigning the Tracking ID dynamically at runtime. This approach is useful if you have several GA accounts but don't want a unique player for each.
When adding GA dynamically, you will not add the Google Analytics plugin in Video Cloud Studio. If you configure it in the Studio, it will be initialized on player load and it will be too late to change the settings (they cannot be overridden).
Another benefit of this approach is that our API integration will not create fields in your GA account (Google won't let you rename existing fields).
When you replace the tracker
value in this example with your own Google Analytics Tracing ID, then you should see metrics on your Google Analytics page.

See the Pen Google Analytics Advanced Integration by Brightcove Learning Services (@bcls1969) on CodePen.
Source code
You can view the source code by selecting the HTML, CSS and JS buttons in the CodePen above, or you can view the complete solution on GitHub.
Using the CodePen
Development sequence
API/Plugin resources used
API Methods | API Events |
---|---|
bcGa() |
Player/HTML configuration
This section details any special configuration needed during player creation. In addition, other HTML elements that must be added to the page, beyond the in-page embed player implementation code, are described.
Player configuration
A Brightcove Player is used for this example.
- In Video Cloud Studio, navigate to the Players module. Create a new player.
-
In the left side navigation, expand Third-Party Analytics. For the GoogleAnalytics option, DO NOT select Enable Google Analytics. Leave this option unchecked.
Google Analytics in Studio
Other HTML
A script was added after the Brightcove Player embed code.
-
After the Player embed code, add the following script for the Brightcove Google Analytics plugin. We are loading the plugin manually instead of configuring it in Video Cloud Studio.
<script src="//players.brightcove.net/videojs-bc-ga/1/videojs-bc-ga.min.js"></script>
Application flow
The basic logic behind this application is:
- Create a Brightcove Player without configuring the Google Analytics plugin
- Load the Google Analytics plugin manually
- Define options for the plugin, including parameters and events that you want to track
- Set the tracking id
- Play your video
-
Create the player
If you have already done so, see the Player/HTML configuration section above to create your player in Studio.
-
Load the plugin
See the Other HTML section above to add the Google Analytics plugin script.
-
Define plugin options
In the JS section of the CodePen, find the code where which is labeled:
// Set up the Google Analytics plugin options
The player calls the bcGa() method to initialize the plugin. Notice that you can set parameters and events that you want to track.
myPlayer.bcGa({ "paramsToTrack": { "bcvideo_video_seconds_viewed": "dimension7", "bcvideo_range": "dimension8", "bcvideo_video_duration": "dimension9", "bcvideo_player": "dimension10", "bcvideo_account": "dimension11", "bcvideo_session": "dimension12", "bcvideo_platform_version": "dimension13" }, "eventsToTrack": { "video_impression": "Video Impression", "play_request": "Play Request", "video_engagement": "Video Engagement", "ad_start": "Ad Start", "ad_end": "Ad End", "player_load": "Player Load", "error": "Error" }, "tracker": "UA-150904906-1" }) /* Two extra events can be added: video_view video_complete */
For details about events and dimensions, see the Configuring Google Analytics Event Tracking document.
-
Set the tracking id
In the JavaScript code above, replace the
tracker
value with your Google Analytics Tracking ID."tracker": "your tracking id"
-
Put the HTML and JavaScript code on your web page and play your video. You should see metrics in your
Plugin code
Normally when converting the JavaScript into a Brightcove Player plugin nominal changes are needed. One required change is to replace the standard use of the ready()
method with the code that defines a plugin.
Here is the very commonly used start to JavaScript code that will work with the player:
videojs.getPlayer('myPlayerID').ready(function() {
var myPlayer = this;
...
});
You will change the first line to use the standard syntax to start a Brightcove Player plugin:
videojs.registerPlugin('pluginName', function(options) {
var myPlayer = this;
...
});
As mentioned earlier, you can see the plugin's JavaScript code in this document's corresponding GitHub repo: advanced-ga.js.
Using the plugin with a player
Once you have the plugin's CSS and JavaScript files stored in an Internet accessible location, you can use the plugin with a player. In Studio's PLAYERS module you can choose a player, then in the PLUGINS section add the URLs to the CSS and JavaScript files, and also add the Name and Options, if options are needed.