Video tracking

Hello together,

who can i track video (youtube, flowplayer) with Piwik?

I found this script to get the stats of a youtube video:

var youTubePlayerLoggers = {}; //
var totalvideos = 0;
var videoid = new Array();
var videoplayed = new Array();

function onYouTubePlayerReady(id)
	var video = document.getElementById(id);

	//State constants
	var unstarted = -1;
	var ended 	  = 0;
	var playing   = 1;
	var paused    = 2;
	var buffering = 3;
	var cued      = 5; //Page loaded, video has not been played yet

	//Video array default values being set for first time
	videoid[totalvideos] = id;
	videoplayed[totalvideos] = false;

	totalvideos = totalvideos + 1;

	youTubePlayerLoggers[id] = function(state)
		document.getElementById("state").innerHTML="youTubePlayerLoggers[id]"; //FOR TESTING

		var el = document.getElementById(id);
		var url = "" + getYoutubeVideoID(el.getVideoUrl());
		var arrayindex = arraySearch(id);
		var currenttime = Math.ceil(el.getCurrentTime());

		if (state == playing)
			if (videoplayed[arrayindex] == false)
				document.getElementById("state").innerHTML = "URL = "+url+"<br />State = "+state+"<br />Value = unique_play_per_page"; //FOR TESTING
			else {
				document.getElementById("state").innerHTML = "URL = "+url+"<br />State = "+state+"<br />Value = play"; //FOR TESTING
			//Flag video as having been played once
			videoplayed[arrayindex] = true;

		if (state == ended)
			document.getElementById("duration").innerHTML = "Duration = "+currenttime; //FOR TESTING
			document.getElementById("state").innerHTML = "URL = "+url+"<br />State = "+state+"<br />Value = ended"; //FOR TESTING

		if (state == paused)
			document.getElementById("duration").innerHTML = "Duration = "+currenttime; //FOR TESTING
			document.getElementById("state").innerHTML = "URL = "+url+"<br />State = "+state+"<br />Value = paused"; //FOR TESTING

	video.addEventListener("onStateChange", "youTubePlayerLoggers."+id);
	video.addEventListener('onError', 'onPlayerError');

function onPlayerError(errorCode)

function getYoutubeVideoID(url)
	var urlArr = url.split("v=");
	var urlArr2 = urlArr[1].split("&");
	return urlArr2[0];

function arraySearch(what)
	for (var i=0, len=totalvideos; i<len; ++i)
		if(videoid[i].indexOf(what) >= 0) return i;

If state equal play i will use a custom variable, but it this the best way?
It is better to use a goal? or a combination?

I want to track the current time and if the video ends.
Can i overwrite the custom variables (current time), if i had set they?

And can i track the current time if the user leave the page (or close the window)?



We now have a Media Analytics plugin that tracks Videos & Audio!

More information is available here: The plugin gives useful insights into media / video / audio analytics and adds many new reports, widgets and segments to Piwik.

Works with HTML5 video & audio, Vimeo and YouTube out of the box. Support for other players can be added easily and is documented in the developer docs. We are also happy to add support for more players upon request, please ping us. After installation the plugin will in most cases directly start tracking data see the setup guide

For a full list of features check out . Docs are available at FAQ, User Guide, Developer docs