PDA

View Full Version : Google Analytics and flash event tracking


alexmel7
05-15-2008, 04:24 PM
Hey, I'm trying to track flash events on my website using the Externalinterface method. This is the Actionscript I have right now but it's not working. Any suggestions?

stop();

import flash.events.MouseEvent;
import flash.external.ExternalInterface;

WC1.addEventListener(MouseEvent.CLICK,WC1Click);
RN1.addEventListener(MouseEvent.CLICK,RN1Click);
CC1.addEventListener(MouseEvent.CLICK,CC1Click);


function WC1Click(event:MouseEvent):void {
gotoAndPlay("WC Open");
}
function RN1Click(event:MouseEvent):void {
gotoAndPlay("WCRN Open");
}
function CC1Click(event:MouseEvent):void {
gotoAndPlay("WCRNCC Open");
}


function logAction(action:String):void
{
action = "/flash/" + action;
ExternalInterface.call("pageTracker._trackPageview",WC1Click);
ExternalInterface.call("pageTracker._trackPageview",RN1Click);
ExternalInterface.call("pageTracker._trackPageview",CC1Click);
}

Gibbah
05-15-2008, 04:49 PM
Does not the logAction function be called on from somwhere?

alexmel7
05-15-2008, 07:37 PM
I was trying to incorporate the method from this website (http://blog.martinlegris.com/?p=86) using their code:

// these are to be imported if you prefer navigateToURL instead of ExternalInterface
import flash.net.navigateToURL;
import flash.net.URLRequest;

// there are to be imported if you prefer ExternalInterface.. my choice..
// the other option drives you nuts while testing from Flash CS3 because it keeps
// opening browser windows each time a call is sent.
import flash.external.ExternalInterface;

public function logAction(action:String):void
{
// add the "/launchpad/" prefix to all actions
action = "/launchpad/" + action;

// the navigateToURL way
navigateToURL(new URLRequest("javascript:pageTracker._trackPageview('"+action"')"), "_self");

// the ExternalInterface way
ExternalInterface.call("pageTracker._trackPageview",action);
}

tsj4
05-16-2008, 01:35 AM
the second param in the external interface calls should be the arguments which are being passed to in to the tracking function which is probably a javascript functions . I didn't look at the link but I have done some tracking.


import flash.events.MouseEvent;
import flash.external.ExternalInterface;

WC1.addEventListener(MouseEvent.CLICK,WC1Click);
RN1.addEventListener(MouseEvent.CLICK,RN1Click);
CC1.addEventListener(MouseEvent.CLICK,CC1Click);


function WC1Click(event:MouseEvent):void {
ExternalInterface.call("pageTracker._trackPageview","WC Open");
}

function RN1Click(event:MouseEvent):void {
ExternalInterface.call("pageTracker._trackPageview","WCRN Open");
}

function CC1Click(event:MouseEvent):void {
ExternalInterface.call("pageTracker._trackPageview","WCRNCC Open");
}



by looking at your example it looks as if _trackPageview(args:String) is a method of the pageTracker() class and is expecting one argument, correct?

alexmel7
05-16-2008, 07:14 AM
yes, this is my javascript, should i change it:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4365601-1");
pageTracker._initData();
pageTracker._trackPageview();
var flTracker = pageTracker._createEventTracker("flash");
</script>

alexmel7
05-16-2008, 07:20 AM
Ok, I changed the actionscript code to what you said. Now this is my javascript:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4365601-1");
pageTracker._initData();
pageTracker._trackPageview();
var flTracker = pageTracker._createEventTracker("WC Open");
var flTracker = pageTracker._createEventTracker("WCRN Open");
var flTracker = pageTracker._createEventTracker("WCRNCC Open");
</script>

alexmel7
05-16-2008, 02:56 PM
It still does not seem to be tracking my flash events:

stop();

import flash.events.MouseEvent;
import flash.external.ExternalInterface;

WC1.addEventListener(MouseEvent.CLICK,WC1Click);
RN1.addEventListener(MouseEvent.CLICK,RN1Click);
CC1.addEventListener(MouseEvent.CLICK,CC1Click);


function WC1Click(event:MouseEvent):void {
gotoAndPlay("WC Open");
ExternalInterface.call("pageTracker._trackPageview","WC Open");
}
function RN1Click(event:MouseEvent):void {
gotoAndPlay("WCRN Open");
ExternalInterface.call("pageTracker._trackPageview","WCRN Open");
}
function CC1Click(event:MouseEvent):void {
gotoAndPlay("WCRNCC Open");
ExternalInterface.call("pageTracker._trackPageview","WCRNCC Open");
}

tsj4
05-16-2008, 07:11 PM
looks as if the value that was being passed to the tracking function was missing this "/flash/". Now try


import flash.events.MouseEvent;
import flash.external.ExternalInterface;

// -----------
WC1.addEventListener(MouseEvent.CLICK,WC1Click);
RN1.addEventListener(MouseEvent.CLICK,RN1Click);
CC1.addEventListener(MouseEvent.CLICK,CC1Click);

// -----------
function WC1Click(event:MouseEvent):void
{
gotoAndPlay("WC Open");
logAction("WC Open");
}

function RN1Click(event:MouseEvent):void
{
gotoAndPlay("WCRN Open");
logAction("WCRN Open");
}

function CC1Click(event:MouseEvent):void
{
gotoAndPlay("WCRNCC Open");
logAction("WCRNCC Open");
}

// -----------
function logAction(arg:String):void
{
action = "/flash/" + arg;
ExternalInterface.call("pageTracker._trackPageview",action);
}

// -----------
stop();



ps. to include ActionScript in a posting use the [as ]....[/as ] tags. just remove the extra space

alexmel7
05-16-2008, 10:08 PM
Thanks tsj4, but now I am getting two compiler errors that say "access of undefined property action".

alexmel7
05-16-2008, 10:18 PM
what if I replace "arg" with "action"

alexmel7
05-16-2008, 10:32 PM
Seems to be working now (checked it in firefox using firebug). Thanks so much tsj4! Greatly appreciate it.

Alex

tsj4
05-17-2008, 01:09 AM
np, glad to help out.