开发者

What is the best way to implement google analytics tracking for Actionscript 2?

I am looking for code, examples, library, components for using google analytics event tracking with my Actionscript 2 Flash movies. I can find info about AS3 on the google code site but not AS2. What is the best resource for tutorials and examples about tagging my Flash files to use the asynch google analytics开发者_JAVA技巧 code. I have found some old information about the old google analytics code.

thanks


I've had to do a lot of mainenance on AS2 projects, so I know where you're coming from. Here's what I do:

Step 1 is to get a google analytics tracking beacon set up in your HTML that your flash movie can use. Google has examples of how to do this, but here's an example of a set up that I did recently:

<!-- Set up Google Analytics tracking -->
    <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">

        // I'm pulling in my project id tag from a config file
        // you will want to use the project id that google gives you. Ex: UA-#####-#

        var pageTracker = _gat._getTracker('<?php echo $config['_tracker']; ?>');
        pageTracker._initData();

    </script>
<!-- End Google Analytics setup -->

With the beacon set up on your page, you can now use ExternalInterface to have your flash movie send tracking messages to google. Somewhere in your flash you'll need a function that you can call up from anywhere else in your code that looks like this:

function track(event:String) {    
    if(ExternalInterface.available) {
        ExternalInterface.call("pageTracker._trackPageview", event);
    }
}

When you want to track an event, you pass in a string that accurately describes the event using google analytics syntax. Ex: /root/loadingFinished or something similar.

Hopefully this helps! Good luck!


I have added this to my Flash projects and it seems to work so far. I have not left it long enough to check what has come through on google analytics reports yet but I will update this answer with what comes through.

import flash.external.ExternalInterface;
//
function ga_track_pageview(_event:String) {
     if(ExternalInterface.available) {
        ExternalInterface.call("_gaq.push",['_trackPageview', _event]);
    }   
}
function ga_track_event(_category:String, _action:String, _label:String, _value:Number) {
     if(ExternalInterface.available) {
        ExternalInterface.call("_gaq.push",['_trackEvent', _category, _action, _label, _value]);
    }   
}
//
// Button 1 pressed - 
btn_1.onRelease = function() {
    _root.ga_track_event("button", "pressed", "button1", null);
}
// Button 2 pressed - 
btn_2.onRelease = function() {
    _root.ga_track_event("button", "pressed", "button2", null);
}
// Tracking a page view -
ga_track_pageview("testpage_opened");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜