Skip to content

tankvn/videojs-google-analytics

Repository files navigation

videojs-google-analytics

Track Google Analytics events from video.js players

Installation

npm install --save videojs-google-analytics

Usage

To include videojs-google-analytics on your website or web application, use any of the following methods.

<script> Tag

This is the simplest case. Get the script in whatever way you prefer and include the plugin after you include video.js, so that the videojs global is available.

<head>
  ...
  <script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    window.gtag =  function() { dataLayer.push(arguments); }
    window.gtag('js', new Date());

    window.gtag('config', 'GA_TRACKING_ID');
  </script>
</head>
<body>
  ...
<script src="//path/to/video.min.js"></script>
<script src="//path/to/videojs-google-analytics.min.js"></script>
<script>
  var player = videojs('my-video');

  player.googleAnalytics({
    events: {
      name: 'VIDEO NAME',
      category: 'Video Category Demo'
    }
  });
</script>
</body>

Available options

Google Analytics

There are two options you can pass to the plugin. The first is to configure which events you would like to trigger from videojs. This option is an array objects for each event. Each event contains the name of the event triggered by Video.js and a label and action which will be sent to Google Analytics. Choose from the list below:

player.analytics({
  events: {
      name: 'VIDEO NAME',
      category: 'Video Category Demo'
  }
})

Browserify/CommonJS

When using with Browserify, install videojs-google-analytics via npm and require the plugin as you would any other module.

var videojs = require('video.js');

// The actual plugin function is exported by this module, but it is also
// attached to the `Player.prototype`; so, there is no need to assign it
// to a variable.
require('videojs-google-analytics');

var player = videojs('my-video');

player.googleAnalytics({
  events: {
      name: 'VIDEO NAME',
      category: 'Video Category Demo'
  }
});

RequireJS/AMD

When using with RequireJS (or another AMD library), get the script in whatever way you prefer and require the plugin as you normally would:

require(['video.js', 'videojs-google-analytics'], function(videojs) {
  var player = videojs('my-video');

  player.googleAnalytics({
    events: {
        name: 'VIDEO NAME',
        category: 'Video Category Demo'
    }
  });
});

License

MIT. Copyright (c) Tank.vn <tanvd357@gmail.com>