Lions Den

The Code and Times of Hanan Schwartzberg

About Hanan | Hanan's CV | Contact Hanan

August 2, 2016

How to track embeded Youtube videos with Google Universal Analytics event

Filed under: Google,JavaScript — Tags: , , , , — Hanan Schwartzberg @ 10:22 am

The problem

You have a Youtube video embedded in an iframe and you need to track it via Google Analytics

The Solution

Since it’s in an iframe you need to use the Youtube API to handle the different events. This requires a few steps.

  1. Include this parameter in the src of the iframe
    ?enablejsapi=1
  2. Include the Youtube API script file
    var tag = document.createElement('script');
    tag.id = 'iframe-demo';
    tag.src = 'https://www.youtube.com/iframe_api';
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  3. Assign the event handler to the required events. In this case the iframe’s ID is frmVideo.
    var player;
    function onYouTubeIframeAPIReady(event) {
        player = new YT.Player((event ? event.id : 'frmVideo')), {
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    }
  4. Actually handle the events
    var pauseFlag = false;
    function onPlayerStateChange(event) {
        // track when user clicks to Play
        if (event.data == YT.PlayerState.PLAYING) {
            ga('send', 'event', 'Video', 'Play', event.target.getVideoData()["title"]);
            pauseFlag = true;
        }
        // track when user clicks to Pause
        if (event.data == YT.PlayerState.PAUSED && pauseFlag) {
            ga('send', 'event', 'Video', 'Pause', event.target.getVideoData()["title"]);
            pauseFlag = false;
        }
        // track when video ends
        if (event.data == YT.PlayerState.ENDED) {
            ga('send', 'event', 'Video', 'End', event.target.getVideoData()["title"]);
        }
    }
  5. On my site the API continuously loaded before the video resulting in it not finding the video in step 3. The other problem was that it needed to have the same name for ever video and would only work for one video on a page. Instead, include an onload event on the iframe and pass the iframe itself in as the event and just default to ‘frmVideo’ if no video was passed in

    onload="onYouTubeIframeAPIReady(this)"

    Downloads

    Right click here and select 'Save link as...' to download this code.

    References

July 17, 2012

Google Custom Search Engine – Error: ‘this.zd’ is null or not an object

Filed under: Google,JavaScript — Tags: , , , — Hanan Schwartzberg @ 4:36 pm

The Issue

When using the Google Custom Search Engine control and executing a default query it throws a JavaScript error “Error: ‘this.zd’ is null or not an object”.


Error: 'this.zd' is null or not an object

The Problem Code

google.load('search', '1');
 
function OnLoad() {
  // Create a custom search control that uses a CSE restricted to code.google.com
  var customSearchControl = new google.search.CustomSearchControl('012157912978810372049:-cv6ao3zqua');
 
  // run a query
  customSearchControl.execute('ajax api');
 
  // Draw the control in content div
  customSearchControl.draw('content'); 
}
google.setOnLoadCallback(OnLoad);

The Solution

Sometimes the most obvious mistakes are the hardest to find. After several Google searches it started to seem like no one else ever had this problem. Threre was no obvious connection between this error and the Google CSE. After staring at the examples here http://code.google.com/apis/ajax/playground/#custom_search_control for a while the answer became obvious.

Before executing the default query the control must first be drawn. The last two commands in the OnLoad method in the above code need to be flipped. Here is the correct code:

google.load('search', '1');
 
function OnLoad() {
  // Create a custom search control that uses a CSE restricted to code.google.com
  var customSearchControl = new google.search.CustomSearchControl('012157912978810372049:-cv6ao3zqua');
 
  // Draw the control in content div
  customSearchControl.draw('content'); 
 
  // run a query (ONLY AFTER DRAWING THE CONTROL)
  customSearchControl.execute('ajax api');
}
google.setOnLoadCallback(OnLoad);
Home | Site Design | Banner Design | Code Den | Offsite Posts | Downloads | Photography | About Hanan | Hanan's CV | Contact Hanan
Copyright © 2009 Hanan Schwartzberg. All rights reserved.