Author Topic: Google Analytics vs Megazine anyone?  (Read 4557 times)

james4551

  • Newbie
  • *
  • Posts: 15
Google Analytics vs Megazine anyone?
« on: October 08, 2009, 04:16:43 pm »
OK so anyone using google analytics with Megazine? I'm trying to track the outgoing clicks from megazine, so I make each page an swf , which each contain 1 or more buttons that are aimed to navigate to a url. So do I need to put the code inside each swf page to track each of these individually. Or can this be set up and exported from the megazine.fla http://code.google.com/p/gaforflash/

Can someone guide me (walk me through) in how I would go about this please? ???
Quote
So far I have thought of adding this to each button....
myButton.addEventListener( MouseEvent.CLICK, onButtonClick );
function onButtonClick( event:Event ):void
{   
  tracker.trackPageview( "/Megazineclicked");
}

myButton.addEventListener(MouseEvent.MOUSE_UP,goThere);
function goThere(e:MouseEvent){
    var request:URLRequest = new URLRequest("http://www.myurl.com/");
    navigateToURL(request);
}

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #1 on: October 08, 2009, 09:02:51 pm »
Interesting. Assuming you're using v2, I think there might be a very elegant solution: write a plugin which overrides the default protocol handling implementations of the links plugin. Allright, that may sound scary, but it really isn't. ;)

Basically you could create a plugin named "googleanalytics" or so, which depends on the "links" plugin:
Code: (actionscript3)
  1. public class GoogleAnalytics extends AbstractPlugin {
  2.    public function GoogleAnalytics() {
  3.        super("googleanalytics", "1.0.0", [new Dependency("links")]);
  4.    }
  5. }

Then write the custom handler (just copy the default one from the links plugin and extend it with the tracker functionality), e.g.
Code: (actionscript3)
  1. /**
  2.  * Google Analytics enabled handler for http and similar urls (common protocols that can be
  3.  * handled by navigateToURL).
  4.  *
  5.  * @param url
  6.  *              the url to navigate to.
  7.  * @param source
  8.  *              optional, element triggering the link.
  9.  * @param target
  10.  *              optional, used e.g. for http:// links.
  11.  */
  12. private function trackingHandler(url:String, source:IElement = null, target:String = null):void {
  13.    target = source
  14.            ? getElementProperty(source, "target")
  15.            : target;
  16.    if (!target) {
  17.        target = "_blank";
  18.    }
  19.    navigateToURL(new URLRequest(url), target);
  20.    tracker.trackPageview("/Megazineclicked"); // <-- what you wrote, I don't know the ga API ;)
  21. }

And finally, in the registration phase of your plugin, override the handlers , like so
Code: (actionscript3)
  1. override public function register(manager:IPluginManager, megazine:IMegaZine, settings:ISettings):void {
  2.    super.register(manager, megazine, settings);
  3.  
  4.    var links:ILinks = pluginManager.getPlugin("links") as ILinks;
  5.    if (links) {
  6.        links.registerProtocol("http://", trackingHandler);
  7.        links.registerProtocol("https://", trackingHandler);
  8.        links.registerProtocol("ftp://", trackingHandler);
  9.        // And what else you might need / want to track
  10.    }
  11. }


And that would pretty much be it...

Would love to hear if it works, and if it does, posting the plugin to the user contributions section (or in the project wiki) would be appreciated ;D

If all your links are in your swfs and not linked elements that won't work, though. You might consider letting them use the links plugin to handle the links, though, then it would.
« Last Edit: October 08, 2009, 09:09:14 pm by hnuecke »
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

james4551

  • Newbie
  • *
  • Posts: 15
Re: Google Analytics vs Megazine anyone?
« Reply #2 on: October 12, 2009, 04:25:10 pm »
Wow that's one in depth response! Thanks a load Florian, if you were here I'd buy you many a beer haha I love this megazine system, good work on that mate.
I've been reading over and over it trying to get my head around AS3, I unfortunately am too flash retarded so far to implement this but if anyone out there could help step me through it I would love to get this feature working.
A good reference to the google analytics and flash integration is here http://code.google.com/apis/analytics/docs/tracking/flashTrackingIntro.html
This would be a great feature to track what your end users are clicking on and what content they like the best (Eg: Number of video plays etc.)
I'm going to keep trying to get my head around on exactly where I need to put all this code etc and will let you know if I figure it out, but I'm sure there's some of you that could do it in 5 seconds rather than 5days like myself haha.

Also just a quick question, I know how to link images using the <url= "linkhere.html"> in the xml file. But if you were creating say animated buttons as swf's, what would be the as to allow them to work? In AS2 I know it would be something like this exported with each button:
Quote
clickety.onRelease = function ()  {
getURL("http://vans.com","_blank");
}
Thanks All.

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #3 on: October 12, 2009, 08:32:03 pm »
Thanks for the link, will look into it one day ;)

As for the linking in the swfs: to have the whole linking centralized (and thus make such things as the google tracking easier) I'd recommend hooking them up with the engine and leaving the actual link handling to the links plugin. There's a tutorial on engine interaction in loaded swfs, but real quick:
Code: (actionscript3)
  1. // this goes inside the script part of the first frame of your swf
  2. // I'm leaving the actual types away here, so you don't need the imports
  3.  
  4. // A reference to the links plugin.
  5. var linksPlugin:*;
  6.  
  7. // This gets called by the engine when the swf is loaded
  8. function megazineSetup(megazine:*):void {
  9.    linksPlugin = megazine.pluginManager.getPlugin("links");
  10. }
  11.  
  12. // Use this function to handle links, it makes sure the links plugin is loaded,
  13. // and if it isn't uses the default navigateToURL (which is basically as3's getURL)
  14. function handleLink(url:String, target:String = "_blank"):void {
  15.    if (linksPlugin) {
  16.        linksPlugin.processLink(url, target);
  17.    } else {
  18.        navigateToURL(new URLRequest(url), target);
  19.    }
  20. }
  21.  
  22.  
  23. // Now your example would look like this (there's no onRelease in AS3, looks like this)
  24. clickety.addEventListener(MouseEvent.CLICK, function(e:Event):void {
  25.    handleLink("http://vans.com", "_blank");
  26. });
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

james4551

  • Newbie
  • *
  • Posts: 15
Re: Google Analytics vs Megazine anyone?
« Reply #4 on: October 13, 2009, 02:51:54 pm »
A-MAZE-ING, that worked perfectly Florian, your a freakin' genious! I'd just thought I'd give you an insight into my progress and get your opinion/help  ;D So now with your script for swf links  i have tried this:
Code: [Select]
      var linksPlugin:*;
      function megazineSetup(megazine:*):void {
         linksPlugin = megazine.pluginManager.getPlugin("links");
      }
      function handleLink(url:String, target:String = "_blank"):void {
         if (linksPlugin) {
             linksPlugin.processLink(url, target);
         } else {
             navigateToURL(new URLRequest(url), target);
         }
      }
        function trackGA(trackingString:String):void{
var targetURL:URLRequest = new URLRequest("javascript:pageTracker._trackPageview('/root/"+trackingString+"');");
sendToURL(targetURL);
}
  clickety.addEventListener(MouseEvent.CLICK, doIt);
  clickety.addEventListener(MouseEvent.MOUSE_UP, doIt);
  function doIt(event:MouseEvent):void{
      switch (event.type){
  case MouseEvent.CLICK: trackGA("/newsletterlink_vans");
  break;
  case MouseEvent.MOUSE_UP: handleLink("http://vans.co.uk", "_blank");
  break;
}
      };
I'm hoping that tracks the outgoing URL as the name newsletterlink_vans in google analytics!

Event tracking is a different matter, so  I don't know if I'm on the right track????
But this guy seems like he has got it, but I'm still working on integrating it:
Code: [Select]
/*String   category The general event category (e.g. "Videos").
   String   action The action for the event (e.g. "Play").
   String   opt_label An optional descriptor for the event.
    Int      opt_value An optional value to be aggregated with the event.
*/
function gaTrackEvent(category:String = null, action:String = null, opt_label:String = null, opt_value:Number = NaN):void {
trace("gaTrackEvent " + category + " " + action + " " + opt_label + " " + opt_value );
var res:Object;
if (ExternalInterface.available) {
if (isNaN(opt_value)) {
res=ExternalInterface.call("pageTracker._trackEvent",category,action,opt_label);
} else {
res=ExternalInterface.call("pageTracker._trackEvent",category,action,opt_label,opt_value);
}
trace("got return value " + res);
}
}
 
function gaTrackPageView(page:String):void {
if (ExternalInterface.available) {
var res:Object=ExternalInterface.call("pageTracker._trackPageview",page);
trace("got return value " + res);
}
 
}
Then you'd call those functions with some other code (e.g. hooked up to a button) like
Code: [Select]
function onSentToOrderPage(evt:Event = null) {
trace("onSentToOrderPage");
gaTrackEvent("SalesEvents","SentToOrderPage"); // at least 2 arguments needed for events
}
Would love to hear your thoughts mate!
 For the above explained :refer to http://troyworks.com/blog/2009/09/06/quickstart-google-analytics-with-flash-flex-and-actionscript/


« Last Edit: October 13, 2009, 02:53:48 pm by james4551 »

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #5 on: October 13, 2009, 07:56:52 pm »
Because it's faster... I committed a draft of a googleanalytics plugin to the SVN:
http://svn.mightypirates.de/megazine/trunk/src/de/mightypirates/megazine/plugins/googleanalytics/GoogleAnalytics.as

Basically this should track all links handled through the links plugin. The plugin adds two settings:
book@gaaccount - the google analytics account name, something of the format of UA-123-456.
book@gatrackas - tracks all links going out from the engine as the same, i.e. does not call trackPageview(urlOfTheLinkClicked) but trackPageview(valueOfThisSetting).

I have not tested this, and no idea if it works as it should, but have a go. Attached is the compiled version.
To track links just use the code I posted previously, i.e. use the links plugin to process links.
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

james4551

  • Newbie
  • *
  • Posts: 15
Re: Google Analytics vs Megazine anyone?
« Reply #6 on: October 14, 2009, 01:01:48 pm »
Nice one Florian your a legend ;D.
OK just so I'm clear (and for anyone interested), I included googleanalytics in my plugins, then I put my gaaccount="UA-6577xxx-1" and gatracks="clicksOutgoing" in my book section in the .mz3 file.

I'm now going to put this up as a test and should get some feedback in the next few days from google analytics to see if it's working. My only concern is if you enter a value for gatrackas such as gatrackas "clicksOutgoing" would this then mean that all out going links will be labeled as clicksOutgoing? As you would want them to be identifiable as well ie: clicksOutgoing+destinationURL or name so you can track which links are actually getting clicked.
My previous post listed the way to track events with "pageTracker._trackEvent" giving you the ability to name your events when called such as gaTrackEvent("buttonClicked","Outgoing", "nameOfURL.com");
Would this work better do you think?

I must apologize for my inept ability to implement these myself, I understand the concept but when it comes to hand coding I'm like a monkey with no digits trying to peel a plastic banana.

Thanks so much for your help and a donation will be made on my part next payday :D.

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #7 on: October 14, 2009, 04:02:54 pm »
Indeed, it'll all be logged as the same thing when using trackas.
Added a setting named gatrackevent.
Usage is simple: define a comma separated list of Strings that should be passed to the trackEvent function, with two available "variables", {url}, the URL that is being navigated to, and {target}, the target parameter of the link. So for your example:
gatrackevent="buttonClicked,Outgoing,{url}"
The fourth parameter, if given, will be converted to a number (as that's what the function of the ga API takes).

New version attached. Make sure you clear your cache so the new plugin version gets loaded!
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

james4551

  • Newbie
  • *
  • Posts: 15
Re: Google Analytics vs Megazine anyone?
« Reply #8 on: October 15, 2009, 10:51:51 am »
Florian you are a genius, I can report for all that the google analytics plugin is definitely working!!! Thank you so much for your support on this mate, it is going to come in well handy to see how users interact with the pages.
I'm sure I'll be back on here soon with more questions haha but I hope that some of my posts will be contributions too.
Keep up the great work Florian.
Cheers ;D

vincent

  • Full Member
  • ***
  • Posts: 61
Re: Google Analytics vs Megazine anyone?
« Reply #9 on: February 08, 2010, 11:00:03 am »
hello,
i need more description for use the ga plugins.
I add to my book definition :
Code: [Select]
<book plugins="googleanalytics" gaaccount="UA-123-456" gatrackevent="buttonClicked,Outgoing,{url}" />
then it's all i need to do ?

for example in my swf (page - img) i do a button with an actionscript: navigateToURL(), when i use this button, i can see it in googleAnlaytics or i have more things to do ?
can i see on what page the user navigate? (i use the swfadress...)

thanks for your explication for this plugins

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #10 on: February 10, 2010, 06:47:03 pm »
Only links processed by the links plugin are handled by the ga plugin, afaik (unless the ga library somehow manages to catch all navigateToURL calls somehow... by magic. Or something)

So if you have a SWF with links that should also be tracked you'd have to route those links through the links plugin, like so (this goes into your SWF)
Code: (actionscript3)
  1. import de.mightypirates.megazine.interfaces.IMegaZine;
  2. import de.mightypirates.megazine.plugins.links.ILinks;
  3.  
  4. // Var to store reference to the links plugin.
  5. var links:ILinks;
  6.  
  7. function megazineSetup(megazine:IMegaZine):void {
  8.    links = megazine.getPlugin("links") as ILinks;
  9. }
  10.  
  11. function handleClick(e:MouseEvent):void {
  12.    // Instead of:
  13.    // navgiateToURL(new URLRequest("http://domain.tld/"));
  14.    // use
  15.    if (links) {
  16.        // Links plugin loaded and found, process link through that.
  17.        links.processLink("http://domain.tld/");
  18.    } else {
  19.        // Links plugin not loaded or found, use normal linking functionality.
  20.        navgiateToURL(new URLRequest("http://domain.tld/"));
  21.    }
  22. }
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

MegaMan

  • Newbie
  • *
  • Posts: 12
Re: Google Analytics vs Megazine anyone?
« Reply #11 on: February 18, 2010, 09:53:13 pm »
If I may ask to expand on this - should the google analytics code also be included at the bottom of your index.html page? Or only in the book code?
Secondly, if I use gatrackevent="buttonClicked,Outgoing,{url}"

Will this track all events processed through the links plugin if I use a swf? What is the meaning of "buttonClicked" and "Outgoing" - is it generic or are those the name of functions I have to use?

Thanks!

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #12 on: February 19, 2010, 04:23:19 pm »
You won't need the JavaScript block in your page.
As for the arguments, it uses this function: http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEventTracking.html
So basically the first two parameters are just arbitrary values you can use to identify your events later on.
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

MegaMan

  • Newbie
  • *
  • Posts: 12
Re: Google Analytics vs Megazine anyone?
« Reply #13 on: February 22, 2010, 01:30:49 am »
Can you perhaps post some example code to track a page turn. I'm using the following code to turn a page:
links.processLink("javascript:MegaZine.gotoPage(4);void(0);");

how would you track this?

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1985
  • MegaZine3 Developer
    • MegaZine3
Re: Google Analytics vs Megazine anyone?
« Reply #14 on: March 05, 2010, 04:42:06 pm »
The plugin currently doesn't track JS calls. Is GA capable of tracking those?
An alternative (that I'd prefer, to keep the plugins more separated) would
be this: I could add a "custom" tracking event that specifically tracks page
changes (using the "delayed" page change event, so when flipping over
multiple page not every single one would be tracked, but the one where
the user finally stops).
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.