What is the best way to implement google analytics

2019-08-28 20:47发布

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

2条回答
Fickle 薄情
2楼-- · 2019-08-28 21:24

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!

查看更多
老娘就宠你
3楼-- · 2019-08-28 21:51

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");
查看更多
登录 后发表回答