Forum Xamarin.Android

How to implement Google Analytics in Mono for android ?

vzus95vzus95 USUniversity ✭✭


We need analytics for our application, for this we thought of using Google Analytics. How to implement ?


  • GlennWilsonGlennWilson USUniversity ✭✭

    I used Google Analytics in a Mono for Android project recently and it was easy. I created a Mono for Android Java Bindings Library and that worked fine without any transforms.

    Example of Easy Tracker:

     protected override void OnStart ()
          base.OnStart ();
          EasyTracker.Instance.ActivityStart (this); // analytics

    Example of Simple Event Tracking:

     ITracker instance = GoogleAnalytics.GetInstance (myApplicationContext).DefaultTracker;
     instance.TrackEvent (event, pieceName, info, value);
  • vzus95vzus95 USUniversity ✭✭

    Hi Wilson,

    Thanks for your reply. Even i tried in the same way like

    - project 1
    - project 2 (Mono for Android Java Bindings Library)
    In project 2 i have attached libGoogleAnalyticsV2.jar file with this i got the below errors ...

    C:\Mono\SSSSS\Lib\obj\Debug\generated\src\Com.Google.Android.Gms.Common.Util.VisibleForTestingAttribute.cs(11,11): Error CS0234: The type or namespace name 'Runtime' does not exist in the namespace 'Com.Google.Android' (are you missing an assembly reference?) (CS0234) (Lib)

    How to resolve this ? And anything else we should take care before using the code on OnStart().

  • GlennWilsonGlennWilson USUniversity ✭✭

    My generated source does not include the file where you are getting the error. A couple of thoughts:

    I downloaded and used the V2 (beta) sdk. Are you using that or the legacy sdk?

    I did this last November or December and it appears there is an update to the V2 jar file. I'll try to take a look at that later today if see if that works the same for me.

    I am also using Google AdMob in this solution but in a separate project and neither project references the other so I don't think that is relevant.

    Once the binding project works I referenced it in my solution.

    Probably irrelevant aside:
    I also included the analytics jar file in my solution as I believed that was needed at the time (in my bindings project I used the older InputJar instead of EmbedddedJar -- I'll also try later today to see if that makes a difference).

    And, then just a using statement for "Com.Google.Analytics.Tracking.Android;"

  • GlennWilsonGlennWilson USUniversity ✭✭

    I downloaded beta 4 and reproduced the issue you were seeing. I could build the binding project (but I have not tried using it) by making my Transforms Metadata.xml:

        <attr path="/api/package[@name='']/interface[@name='TrackerHandler']" name="visibility">public</attr>
        <remove-node path="/api/package[@name='']" />
  • vzus95vzus95 USUniversity ✭✭

    Thanks Wilson as you said it was not throwing any error now after using your code Metadata.xml ..

    As am new to Mono what is the next step for using Google Analytic s.
    I tried this name but it was not proper ..

    And, then just a using statement for "Com.Google.Analytics.Tracking.Android;"

    I tried of using the below code directly in one of the Activity file.. It showing error at EasyTracker ...

    protected override void OnStart ()

    base.OnStart ();
    EasyTracker.Instance.ActivityStart (this); // analytics

    Direct me how to proceed further.

  • GlennWilsonGlennWilson USUniversity ✭✭

    What error did you get? Is it a build error or runtime error?

    I created a new project from scratch that referenced the new bindings project. The new project compiled fine and ran. To work at runtime you will need to update your manifest for the required permissions and create an analytics.xml (in Resources/values) as described at:

    You will need a GA tracking ID to place into your analytics.xml file.

  • vzus95vzus95 USUniversity ✭✭

    Build Error ! PFA

  • vzus95vzus95 USUniversity ✭✭
  • GlennWilsonGlennWilson USUniversity ✭✭
    1. Reference it in your solution.
    2. You need a using statement.

      using Com.Google.Analytics.Tracking.Android;

    Item 1 above could be accomplished by adding the bindings project to your solution (right-click on the solution in the solution explorer; select add....>Existing project and browse to the bindings csproj file). Then edit references for the project that needs it and select in the projects tab.

  • vzus95vzus95 USUniversity ✭✭

    Thanks Wilson it's working!

  • VikramVarmaVikramVarma INMember ✭✭

    I am getting below error while using the easy tracker. Please Help.

    Thread[GAThread,5,main]: connecting to Analytics service
    Thread[GAThread,5,main]: connect: bindService returned false for Intent { (has extras) }
    Thread[GAThread,5,main]: Service unavailable (code=1), will retry.
    Thread[GAThread,5,main]: No campaign data found.
    Thread[Service Reconnect,5,main]: connecting to Analytics service
    Thread[Service Reconnect,5,main]: connect: bindService returned false for Intent { (has extras) }
    Thread[Service Reconnect,5,main]: Service unavailable (code=1), using local store.
    Thread[Service Reconnect,5,main]: falling back to local store
    Thread[GAThread,5,main]: dispatch running...
    Thread[GAThread,5,main]: ...nothing to dispatch
    Thread[GAThread,5,main]: PowerSaveMode initiated.

  • @Vikram, I get the same error. It happens at random. Did you figure out a solution?

  • StevaSteva RSMember ✭✭✭

    @WilliamGrand Maybe try binding from xamarin samples. I didn't have any problems with this binding.

Sign In or Register to comment.