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

December 5, 2015

Row numbering for ui-grid in Angular JS

Filed under: AngularJS,JavaScript — Tags: , , , — Hanan Schwartzberg @ 9:46 pm

The Issue

You want to display row numbers in a ui-grid. The numbers should should stay sequential regardless of the sorting

The Solution

Add this column to the grid’s columnDefs

{ name: 'rowNum', displayName: 'Row Number', cellTemplate: 
'<div class="ui-grid-cell-contents">{{grid.renderContainers.body.visibleRowCache.indexOf(row) + 1}}</div>' }

November 12, 2015

Default sort for ui-grid in Angular JS

Filed under: AngularJS,JavaScript — Tags: , , , — Hanan Schwartzberg @ 6:12 am

The Issue

You want to use the internal sorting for an ui-grid, but want to have it sort by default by a specific column.

What didn’t work

Including

sortInfo: {fields:['Title'],directions:['asc']},

in the grid definition didn’t work because it requires using external sorting.

The Solution

The default sort order isn’t declared on the grid level. It’s declared in the definition of the column itself. For example:

columnDefs: [
    {
       field: 'Title', displayName: 'Title', width: 160,
       sort: { direction: uiGridConstants.ASC, priority: 1 }
    },
    {
       field: 'Description', displayName: 'Description', width: 160
    }
]

February 12, 2014

Resize an iFrame to match its contents

Filed under: JavaScript — Tags: , , , — Hanan Schwartzberg @ 9:52 am

The Issue

You need to load content into an iFrame, but you don’t know the height of the content ahead of time.

The Solution

Catch the lonload event of the iFrame and resize it to the height of the body of the content

&lt;iframe id="ifArtworkDetails" width="650px" height="420px" scrolling="no" 
        src="someDynamicContent.html" marginheight="0" marginwidth="0" 
        frameborder="0" onload="resizeIframe(this)">&lt;/iframe>
    function resizeIframe(objIframe) {
        objIframe.style.height = 
            objIframe.contentWindow.document.body.scrollHeight + 50 + 'px';
    }

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.