SWFAddress and the new Google Analytics (ga.js)

I’m always playing a bit with SWFAddress, SWFObject and Google Analytics. SWFAddress has a nice features which is that it will advise Google Analytics when you change a page inside your main swf movie. That gives you the ability to do tracking on your website. Pretty neat!

Has some of you might know, Google Analytics has released a new tracker code (from urchin.js to ga.js). What more does it do for you right now? Not much, it’s supposed to be smaller in size and that’s about it. No event yet (there was a lot of buzz around events in Google Analytics and there is actually documentation on how to implement them but they are still in closed beta; we will have to wait on this one), no new functions. But if your launching a new website I would advise putting the new tracker code (ga.js) because it will save you maintenance time when you will want the new features.

So does SWFAddress works with the new code? Well the answer is yes, and it is pretty easy to make it work here is sample code showing you how to do 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 type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-xx");
<script type="text/javascript" src="js/swfobject.js"></script>
<script type="text/javascript" src="js/swfaddress.js?tracker=pageTracker._trackPageview"></script>

The most important part of the code is the last line where you tell SWFAddress which tracker to use. That’s it! I hope this saves you some time.

, , ,

  1. #1 by Gene - February 21st, 2008 at 19:36

    Quick question. Does the tracking code have to be above the two included javascript files for it to work or can I put the tracking code just above the tag like usual?

  2. #2 by Gene - February 21st, 2008 at 19:38

    Oops, it pulled out the body tag from my comment. It was supposed to be the closing body tag I was referring to.

  3. #3 by dgelineau - February 25th, 2008 at 00:01

    Yes the tracking code has to be over the two included javascript files. The reason for that is quite simple, look at the code to include the swf address javascript, it is a bit different than usual and it needs the Google Analytics tracker to be defined before in order to work.

  4. #4 by george - March 3rd, 2008 at 10:47

    HI there i was wondering what version of SWFAddress are you using? 2.0? I cant seem to get it to work.
    so “pageTracker._trackPageview();” is taken out of the code that google recommends to use for ga.js?

    All this code goes before the closing ?

  5. #5 by george - March 3rd, 2008 at 13:00

    sorry for kinda repeating the body tag question, but if, as google suggests, the tracker code goes before the closing body tag and then the swfobject + address follows that, that means your replacement content has to go before all this since it within the body no?

    Its causing my page to display its replacement content for a second then the swf is displayed afterwards.

    I guess you just go against what google says about the ordering?

  6. #6 by dgelineau - March 4th, 2008 at 15:32

    Yep I am using SWFAddress 2.0.

    I have put all the code that I pasted in the header of my HTML document. It is only a sugestion to put the tracker code before the body tag but you can put it where you want.

    Ya, you have to remove the “pageTracker._trackPageview();” from the recommended code from google, but you have to put it in the import statement of the SWFAddress javascript so that it knows to use the ga.js tracker.

    I thought I could just point you to the website I’m using it on so you could just view the source, but it is not online yet… I’ll post it when it is.

    I hope this help.

  7. #7 by estebanz - March 13th, 2008 at 04:57

    the parameter ?tracker=pageTracker._trackPageview seems to work fine on firefox but on IE, it’s very buggy
    I tried to call SWFAddress.setTracker(“pageTracker._trackPageview”); but it does not seems to be working (no call to __utm.gif on google’s server)
    Finnaly I redefine the urchinTracker function :
    function urchinTracker(u){
    not very clean but it works !

  8. #8 by james - April 3rd, 2008 at 16:10

    Has anyone else tried and verified that this works?

  9. #9 by Mark Kemper - April 27th, 2008 at 21:32

    I just used standard script include tags then set the function as below.

    var pageTracker = _gat._getTracker(“UA-31057-60″);

    Seems to work just fine.

  10. #10 by marc - April 22nd, 2009 at 03:03

    I found that in google analytics it doesn’t take “#” to the link it tracks. So when u click “view this page”, it bring u to the wrong page.(normally the page isn’t exist, if u did deep linking)

  11. #11 by Bill Trikojus - May 13th, 2009 at 21:31

    @ Mark Kemper
    Hi Mark,

    Can you post your full code please? Do you replace any of the normal google code with what you posted above or are those 3 lines just added underneath?


  12. #12 by Bill Trikojus - May 13th, 2009 at 22:25

    Please ignore my previous post. Looks like swfaddress now works with GA without having to do anything.

  13. #13 by iongion - June 1st, 2009 at 07:47

    Did all of the above, started charles to monitor tracking.

    No requests to google are made while navigating.

    - SWFAddress 2.3
    - SWFObject 2
    - The new form Google Analytics code

  14. #14 by Bryan - June 10th, 2009 at 14:14

    Hey everyone,

    I’m having a heck of a time getting this to work for some reason. I’ve included the code just as described above but my Google Analytics insists the tracking code isn’t installed.

    At http://www.bryanbeca.ca, view the source. Can anybody help me? I’ll be eternally grateful.

  15. #15 by Zach Foley - July 23rd, 2009 at 13:46

    Why not use ga for flash and track events from there?

  16. #16 by zedia.net - July 23rd, 2009 at 13:48

    @ Zach Foley
    Because GA for Flash didn’t exist at the time I wrote this, events weren’t even out yet. Now it’s a whole new ball game.

  17. #17 by Peter - July 28th, 2009 at 10:15

    @ Mark Kemper

    Hi Mark,

    U want to say that the following code will work?:

    try {
    var pageTracker = _gat._getTracker(“UA-xxxxxxxxx”);
    } catch(err) {}

    Do I have to add some code to awfaddress.js or to leave it as it is? would U show me a website with a working example.

    P.S. I use SWFaddress 2.3 and SWFobject 2 in my head tag.

  18. #18 by Hurby - December 17th, 2009 at 13:23

    Google analytics doesn’t take the “#” to the link it tracks. So when u click “view this page”, it will bring u to the wrong url. Does anybody have a solution.

  19. #19 by Dmitar - June 16th, 2010 at 07:23

    @ Mark Kemper
    Thanks, Mark this worked for me in 2010 :-)

  20. #20 by Murdoch - August 4th, 2010 at 02:01

    Thank you very much. As Dmitar has fairly commented, this code still works prefectly in 2010. Cheers.

  21. #21 by Philip Hodges - November 26th, 2010 at 04:49

    Thanks Mark.

  22. #22 by Mecanographik - January 12th, 2011 at 12:09

    Thanks for this post but here is the last release :
    With the new GA and SWFAddress 2.4, the GA tracking through SWFAddress doesnt work anymore. Thats because the ‘pageTracker’ that SWFAddress uses no longer exists. All you would have to do to make it work is to add the following line at the end of the GA script: :

    var pageTracker = { _trackPageview: function (url) {_gaq.push (['_trackPageview', url ]);}};

    This creates that ‘pageTracker’ that SWFAddress needs to track through GA. So now it looks like this:

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'xx-xxxxxxxx-x']);
    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’;
    ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ :
    ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0];
    s.parentNode.insertBefore(ga, s);

    // Tracker for SWFAddress
    var pageTracker = { _trackPageview: function (url) {_gaq.push (['_trackPageview', url ]);}};

    Just check out the following post:

(will not be published)
Subscribe to comments feed

Parse error: syntax error, unexpected ';' in /homepages/25/d169645162/htdocs/wp-content/themes/fusion/footer.php on line 13