Archive for the 'development' Category

Track JW FLV Player Videos with Google Analytics

Tuesday, October 14th, 2008

The FLV Media Player by Jeroen Wijering can be considered as one of the most advanced commercially available FLV Players on the market. It features nifty functions such as streaming and Javascript callback functions. These callbacks can be used to track how often a video was played with Google Analytics.

The reference docs for the Javascript API are the starting point. But I will show you how to implement the video tracking here.

I recomend setting up a new Javascript source file, let’s call it video.js. Include this file into your page which will show the video:
<script type="text/javascript" src="/scripts/video.js"></script>

Now embed the FLV Player according to the documentation.
Every instance of the FLV player will try to call a Javascript function when the FLV is finished loading: playerReady(obj) .

So we add such a funtion to our video.js:

var player;
function playerReady(obj) {
var id = obj['id'];
player = document.getElementById(id);
};

Now the global variable player holds a reference to the FLV Player object. Now we can add a callback handler to the player object, according to the various callback hooks described in the docs. We want to listen to an event that tells us, when a video has been started. This is the “STATE” hook which fires an event every time the video starts playing, stops or ends.


function playerReady(obj) {
var id = obj['id'];
player = document.getElementById(id);
player.addModelListener("STATE","playTracker");
};

So now every time the state of the video changes, the player tries to call the playTracker function. Let’s implement it.


function playTracker(obj) {
if (obj.newstate=='BUFFERING' && started==false) {
started=true;
pageTracker._trackPageview("/videos/videos/"+obj.id );
}

The player not only calls this function, but also send certain information in the parameter obj. We are interested in the value obj.newstate, which of course tells us the state to which the player just changed. We are only interested if the player just started, because this means the user pressed the “PLAY” button. Please note the variable started which we use to ensure that the play event is only tracked once. The “BUFFERING” event could be fired more than once, if the player needs to reload the video, due to a slow connection or when the user skips the videostream.
Inside the playTracker function we use Google Analytics _trackPageview function to fire the actual tracking of the video. The obj.id variable holds the id of the SWF file. This is initially set when embedding the flash in your HTMl page:

var so = new SWFObject('player.swf','mpl','300','250','9');

In this example the id would be mpl. For the tracker to make sense you need to set this id to some string which identifies the actual video, like a description. If you create this id dynamically, for example from a database, make sure to strip any kind of whitespaces or special characters, since this could break either the Javascript or Google’s tracking.

The complete video.js should look like this:


var player;
var started = false;
function playerReady(obj) {
var id = obj['id'];
player = document.getElementById(id);
player.addModelListener("STATE","playTracker");
};

function playTracker(obj) {
if (obj.newstate=='BUFFERING' && started==false) {
started=true;
pageTracker._trackPageview("/videos/videos/"+obj.id );
}

};

Please note that I have not tested this code with more than one instance of the JW FLV Player but I think it might even work with more.

Remove the "»" (») from your WordPress title

Monday, September 29th, 2008

WordPress puts a “»” (&raquo;) in front of all blog archive entries. This had always bothered me. To remove this character, you have to find the function call to “wp_title()” in your theme. It should usually be in a file called header.php.
In fact when you call this function, you can send it a string, which will be prepended to the title. Unfortunately it is possible to define default parameter values in PHP and a lot of theme developers jsut don’t put any parameter in the wp_title() call. So you don’t know that it’s possible to give another value by the paramter. Just another reason why PHP just plainly sucks at being a serious computer language…

I replace the call to wp_title() with the following code:

<?php trim(wp_title("")); ?>

This removes the » and trims any whitespaces from the title.

BTW: It’s also very bad pratice to have the wp_content() function actually output the title and not having it return a string containing the title. Don’t even get me started about PHP developers…

Disable the iPhone auto screen-lock by API

Thursday, August 14th, 2008

If you are developing iPhone applications you might run into a situation where your application is “closed” by the iPhone’s automatic screen lock, which kicks in after a few minutes, according to the settings by the user. This of course happens when you have no interaction like tapping the screen from the user.
To disable the automatic screen lock you have to add the following line of code to the applicationDidFinishLaunching method of your app delegate.

application.idleTimerDisabled = TRUE;