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

我的梦境 提交于 2020-01-17 03:13:45

问题


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 code. I have found some old information about the old google analytics code.

thanks


回答1:


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!




回答2:


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");


来源:https://stackoverflow.com/questions/6204221/what-is-the-best-way-to-implement-google-analytics-tracking-for-actionscript-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!