How to track a goal in Piwik from the JW Video Player

In my search for a nice tool to view basic statistics how many times a video has been played on my website, I discovered that this is possible using Piwik and JWPlayer combined with the Madlytics plugin,

In this tutorial I will try to explain briefly how to:

  • Set a goal for your website
  • Use JWPlayer to trigger a goal when the video reached the point where it is playing 33%

When you have never used goals in Piwik, you will need to activate the Goal-plugin.
This is done by entering the Settings page and activate the Goals plugin in Plugins Management

Now you will be able to create a goal by going to your Dashboard and select Goals.

Add a new goal and give it the name (e.g.) MyVideo
As from Piwik version 0.4.3, you can now set “Goal is triggered” to “manually”.
Prior version: Goal is triggered when visitors: "Visit a given URL (page or group of pages)"
where the URL “contains” : “MyVideo”

For Piwik, this is just enough. Now comes the part where to figure out which id your newly created goal has.
Under Goals, you will now see your newly created goal “MyVideo”.
When clicking Edit existing goals, you will find the Id of your newly made goal.
This is the number you will need to remember once you want to trigger this goal.

For now, I’ll presume it is goal number 1 (idGoal=1)

To place a video using JWPlayer, you will need to visit their website and read the tutorials there.
But to make a quick jump here, I will place the entire code I use on my page which should be simple enough to copy and edit for your needs.

Notice that there is a second plugin here, which is the Autostarter. A nice plugin which will make sure that repeating visitors will see the video play automatically for only 3 times. After that, it will stop auto-play.

At the bottom, I have made a function based on the Piwik code to track a page, but now to track a goal.
This is an additional function, so you do not have to alter your Piwik code at the bottom!

So get the JWPlayer[/url], the [url=http://code.google.com/p/swfobject/]swfObject 2.x and this code, and off you go!

<div id="vplayer1" align="center"><a href="http://www.adobe.com/go/EN_US-H-GET-FLASH">
    <img border="0" src="http://www.mysite.nl/images/template/get_flash_player.gif" /></a></div>
<script type="text/javascript">
//<![CDATA[
function createPlayer() {
var flashvars = {
'file':'http://www.mysite.nl/images/video/MyVideo.flv',
'autostart':'true',
'repeat':'false',
'plugins':'madlytics-1,autostarter',
'madlytics.callbacktype':'javascript',
'madlytics.callbacklistener':'logme',
'madlytics.completedpoint':'33',
'autostarter.count':'3',
}
var params = {
'allowfullscreen':'true',
'allowscriptaccess':'always'
}
var attributes = {
'id':'vplayer1',
'name':'vplayer1'
}
swfobject.embedSWF("http://www.mysite.nl/functions/player.swf", "vplayer1", "737", "434", "9.0.115", false, flashvars, params, attributes);
}
createPlayer();
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
function logme()
{
try {
 var piwikGoalTracker = Piwik.getTracker(pkBaseURL + "piwik.php", YOURSITE_ID_HERE);
 piwikGoalTracker.trackGoal(1); //Equals your idGoal!
}
catch( err ) {}
}
//]]>
</script>

I can’t believe no one thanked you for this, very nice tutorial man. I know you wrote this awhile ago, but I was trying to find a way to do this, and your tutorial is exactly what I was looking for and it worked perfectly!

Thanks!
In addition to the above, I changed my logme() function to be able to control serveral video’s in e.g. a playlist.

<script type=text/javascript> 
//<![CDATA[
function logme(goal)
{
    var trackfile = goal.file.replace(/http:\/\/www.yourdomain.com\/yourpath\/video\//,"");
    var trackgoalnumber=0;
 
 
if (trackfile == "YourVid01.flv" && goal.state == "COMPLETE(33%)") { trackgoalnumber=14; }
if (trackfile == "YourVid02.flv" && goal.state == "COMPLETE(33%)") { trackgoalnumber=13; }
if (trackfile == "YourVid03.flv" && goal.state == "COMPLETE(33%)") { trackgoalnumber=11; }
if (trackfile == "YourVid04.flv" && goal.state == "COMPLETE(33%)") { trackgoalnumber=9; }
 
    if (trackgoalnumber > 0)
    {
         try {
     //    alert ("Tracking goal number: "+trackgoalnumber+"\nFor file: "+trackfile);
         var piwikGoalTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 2);
         piwikGoalTracker.trackGoal(trackgoalnumber);
         }
         catch( err ) {}
     }
}
//]]>
</SCRIPT>

Big thanks from germany! It works great! Thank you very much!

Where can I find an iphone- or smartphone application for piwik?

virogo, thank you so much for this tutorial…i’ve been pulling my hair out for ages. cheers style_emoticons/<#EMO_DIR#>/smile.gif

Thank you for the tutorial but I have some trouble with it.
Please check videos.dev5-media.de/piwik-test.html. I replaced the original logme-code with an alert box but nothing occurred.

Edit: Found the problem… see Publisher Support | JW Player

Virogo,

Impressive tutorial!

Thanks for it, I will give it a try for sure.

Is the code given in the original post for an html page? Can anyone please tell me where I would put the code on a drupal website?

Thanks

Great, easy to follow tutorial, but it didn’t work for me. And I followed everything to the letter.Replica Watches

Wow…! I can’t believe no one thanked you for this, very nice tutorial man. I know you wrote this awhile ago, but I was trying to find a way to do this, and your tutorial is exactly what I was looking for and it worked perfectly!

Virogo, what versions of the player have you tried?

I tested versions 4.1 - 4.7 (source): they all do unwanted callbacks on 0% and 100% plus the custom % value you set. So instead of the one goal you want it saves three! (JVP v4.1 is an exception, it does only one unwanted callback: on 100%). Also follow the example posted by Chad with madlytics.debug set to true:
http://www.longtailvideo.com/support/forums/addons/using-plugins/9319/madlytics-and-free-player#comment-60203

To solve this issue, I wrapped the Piwik call in a simple if statement and included the object in the function:


function logme(obj)
{
try {
if(obj.state=='COMPLETE(33%)'){
 var piwikGoalTracker = Piwik.getTracker(pkBaseURL + "piwik.php", YOURSITE_ID_HERE);
 piwikGoalTracker.trackGoal(1); //Equals your idGoal!
}
}
catch( err ) {}
}

Now I get only my wanted custom goal saved. Good!
Code tested with JW Player v4.6

My big thanks goes to Virogo for this tutorial.

Edit: did some code completion.

Has anyone gotten this working with version 5 of JW Player?

Virogo, very Impressive tutorial!

Thanks a lot, I will give it a try for sure in my new website.

Thanks from Turkey!!!
it’s very good that this topic moved to tutorials…

Here’s a way to track plays on version 5. Add this code to your jwplayer setup code:


events: {
   onPlay: function() {
      try {
         var piwikGoalTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 2);
         piwikGoalTracker.trackGoal(1);
      }
      catch( err ) {}
   }
}

really very Impressive tutorial! and very simple to follow.

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

More information is available here: http://www.media-analytics.net/ 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 https://plugins.piwik.org/MediaAnalytics . Docs are available at FAQ, User Guide, Developer docs