Skip navigation
Currently Being Moderated

How to send datas properly to Google Analytics (ANE-Google-Analytics)

Nov 20, 2013 2:41 AM

Tags: #ane #googleanalytics

Hello,

I meet a problem when I'm using the native extension created by Alessandro Bianco : ANE-Google-Analytics. I tested this code many times, in the context of AIR mobile application, but I tested it without any success to add new datas to Google Analytic history. I'm really connected to internet properly.

Moreover I think that I've made my functions as required by the author : Can you look at my code below to detect some errors to communicate with Google Analytics ?

 

<s:TabbedViewNavigatorApplication

    xmlns:fx="http://ns.adobe.com/mxml/2009"

    xmlns:s="library://ns.adobe.com/flex/spark"

    creationComplete="creationCompleteHandler(event)"

>

 

    import eu.alebianco.air.extensions.analytics.Analytics;

    import eu.alebianco.air.extensions.analytics.api.ITracker;

   

    import flash.utils.clearInterval;

    import flash.utils.setInterval;

    import flash.utils.setTimeout;

 

    public var ganalytics:Analytics;

    public var gtracker:ITracker;

    protected var i:int = 1; // Debug

   

    protected static const ANALYTICS_ID = "My UA code is here";

 

    protected function creationCompleteHandler(event:Event):void

    {

        // I launch the tracker :

        ganalytics  = Analytics.getInstance();

        ganalytics.dispatchInterval = 5; // Interval for connecting : 5 seconds

        // I begin to use the tracker :

        GtrackerSessionUsing("RecordingView","HomeView",true);

    }

   

    /**

     * GtrackerConnecting

     * @description : Open connection to Google Analytics, and create instance for a tracker

     * @argument : actionName = "RecordView" or "RecordEvent"

     * @argument : valueName = Name of the view or name of the event to record

     **/

    public function GtrackerSessionUsing(actionName:String, valueName:String, createConnection:Boolean=false):void

    {

        gtracker = ganalytics.getTracker(ANALYTICS_ID);

        gtracker.startNewSession();

        if(etablirConnexion==true)

        {

            // Tracking des views :

            var compteur:int = 0;

            var intervalGtrackerCnxOuvrir:uint = setInterval(function():void{

                counter++;

                trace("gtracker ="+gtracker);

                if(counter==50){ // Try to connect - only - 50 times

                    trace("GoogleTracker fails to connect !");

                    clearInterval(intervalGtrackerCnxOuvrir);

                }

                else if(gtracker!=null){

                    trace("GoogleTracker success to connect !");

                    GTrackerRecordAction("EnregistrerView","HomeView");

                    clearInterval(intervalGtrackerCnxOuvrir);

                }

                else{

                    trace("Trying to connect to GoogleTracker... (itérate #"+counter+")");

                    gtracker = ganalytics.getTracker(ANALYTICS_ID);

                    gtracker.startNewSession();

                }

               

            },5000); // All 10 seconds : iterate a new connection test

        }

    }

   

    protected function GTrackerRecordAction(actionName:String, valueName:String)

     * @description : Use the tracker of the Google Analytics connected account, all of Views can call this function with this model of code: parentApplication.GTrackerRecordAction(...)

     * @argument : actionName = "RecordView" or "RecordEvent"

     * @argument : valueName = Name of the view or name of the event to record

     {

        if(gtracker!=null)

        {

            switch(actionName){

                case "RecordView":

                trace("GoogleTracker : gtracker.buildView("+valueName+").track()");

                gtracker.buildView(valueName).track();

                break;

               

                case "RecordEvent":

                switch(valueName){

                    case "event1":

                        gtracker.buildEvent("click", "button").withLabel("play").withValue(10).track();

                    break;

                    case "event2":

                        gtracker.buildEvent("click", "button").withLabel("stop").track();

                    break;

                    case "event3":

                        gtracker.buildEvent("app", "quit").track();

                    break;

                }

                break;

            }

        }

    }

   

Best regards.

 
Replies
  • Currently Being Moderated
    Dec 11, 2013 8:33 AM   in reply to Youtch

    you can check this out  ! http://www.devactionscript.com/free-ane/google-analytics-ane-adobe-air -native-extensions-for-android/ 

    it is based on Google Analytics SDK v3 . I hope it works for you 

     


     
    |
    Mark as:
  • Currently Being Moderated
    Mar 7, 2014 11:18 AM   in reply to Youtch

    Youtch,

         I'm not sure exactly what your issue is, but I was unable to connect and looked at your code, modifying it for my purposes and it fixed my code. Hopefully it can help fix yours.

     

         I did notice one thing as I went through, however:

     

    You use

       var compteur:int = 0;

                var intervalGtrackerCnxOuvrir:uint = setInterval(function():void{

                    counter++;

    The compteur does not match the counter variable. It appears a problem with Franglais is the issue here.

     

    In the event it may help, here is the code I used:

     

    In my creationComplete function:

    GoogleAnalytics.initialize("UA-47303719-1");

     

    Here is the code in my app.xml

    <extensions>

            <extensionID>eu.alebianco.air.extensions.analytics.NativeGATracker</extensionID>

        </extensions>

     

     

    And here is the class I created to use the ANE:

     

    package ANE.NativeGATracker{

         import flash.utils.clearInterval;

         import flash.utils.setInterval;

     

         import eu.alebianco.air.extensions.analytics.Analytics;

         import eu.alebianco.air.extensions.analytics.api.ITracker;

     

         public class GoogleAnalytics{

              static private var _analytics:Analytics;

              static private var _tracker:ITracker;

              static private var _supported:Boolean;

     

              /**

               * There is no need to instantiate this class

               */

              public function GoogleAnalytics(){

              }

              /**

              * Initialize the GoogleAnalytics object.. or return false, if Analytics isn't supported

              */

              static public function initialize(ANALYTICS_ID:String):Boolean{

                   if(Analytics.isSupported()){

                        _analytics  = Analytics.getInstance();

                        _analytics.dispatchInterval  = 5;

                        establishTracker(ANALYTICS_ID);

                        _supported   = true;

                   }else{

                         _supported  = false;

                   }

                   return _supported;

              }

     

              /**

              * Establish the tracker connection

              * @description: establishes the tracker connection... reattempt every 10 seconds, up to 50 times

              */

              static private function establishTracker(ANALYTICS_ID:String):void{

                   _tracker  = _analytics.getTracker(ANALYTICS_ID);

                   _tracker.startNewSession();

                   var counter:int    = 0;

                   var establishInterval:uint  = setInterval(function():void{

                        counter++;

                        trace("Google Analytics tracker = " + _tracker);

                        if(_tracker != null){

                             trace("GoogleAnalytics tracker connected!");

                             _tracker.buildView("Launch").track();

                             _tracker.buildView("Home").track();

                             clearInterval(establishInterval);

                       }else if(counter == 50){

                             trace("GoogleAnalytics failed to connect.");

                             clearInterval(establishInterval);

                       }else{

                             trace("Trying to connect to Google Analytics... attempt #" + counter);

                             _tracker  = _analytics.getTracker(ANALYTICS_ID);

                             _tracker.startNewSession();

                       }

                   }, 5000);

              }

             /**

             * Analytics object

             */

              static public function get analytics():Analytics{

                   return GoogleAnalytics._analytics;

              }

              /**

              * Tracker object

              */

              static public function get tracker():ITracker{

                   return GoogleAnalytics._tracker;

              }

              /**

              * Returns true if analytics are supported, but false if they are not. Check this before any use of the tracker or analytics objects

              */

              static public function get supported():Boolean{

                   return GoogleAnalytics._supported;

              }

         }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points