Android Development Step-by-step 70:android access to Google Analytics summary

Source: Internet
Author: User

Ask for yourself, today's project to access Google Analytics, which is used for statistical analysis, you can view the current mobile phone active users, event clicks and so on data, first look at the effect:

Prior to eclipse inside the access has been successful, yesterday the project team decided to use Android studio to develop, see Google official documents, official document Https://developers.google.com/analytics/ devguides/collection/android/v4/, and then the official document inside the profile is used Google-services.json, which is said to be placed under the app folder, and then before the eclipse How do I use the Analytics.xml under the values folder? The official documents do not have a description, really gnawing father Ah. And then it's https://github.com/googlesamples/google-services on GitHub. This demo is not complete, is the configuration file? Google so large companies to bury so many developers, and then online to find information, looking for a half-day, the Internet has not yet introduced how to access the Ga,android developer QQ Group Consulting also has no response, then self-pondering, finally solved, The so-called Google-services.json is not used at all, and then analytics.xml should be placed under the XML folder. New is just the right time.

   Private Tracker Mtracker;    Synchronized public Tracker Getdefaulttracker () {        if (Mtracker = = null) {            Googleanalytics analytics = Googleanaly Tics.getinstance (this);            To enable debug logging use:adb Shell setprop log.tag.GAv4 debug            mtracker = Analytics.newtracker (r.xml.analytics);        }        return mtracker;    }

OK, this article describes how to access two development tools, one, how to use Eclipse to develop Android apps to access Google Analytics, and how to use Android studio to develop Android apps to access Google Analytics. For everyone's reference.

first, how to use Eclipse to develop Android apps how to access Google Analytics

1: Create Analytics Account

https://www.google.com/analytics/on this site, you can use your Gmail account to log in, then you can go to create your app, step by step, then you will get to a Tracking ID, such as:
Ua-66283842-1, this later requires you to add to the Analytics.xml folder.

2. Download the Google Analytics Services SDK and get the Libgoogleanalyticsservices.jar placed in the Project Libs folder

Https://developers.google.com/analytics/devguides/collection/android/resources?hl=es

3, Androidmanifest.xml Add permissions

<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>

4, configure the Analystics.xml, put in the values folder can be

Analystics.xml content is as follows

<?xml version= "1.0" encoding= "Utf-8"?><resources> <!--the Google Analytics Tracking ID to which you want to send data. The dash in the ID must be encoded without encoding. You can deactivate the trace feature by not providing this value. -<!--lockbooster release version (UA-56887526-12); Dev version (ua-56887526-7) ua-56887526-15--> <string name= "Ga_trackingid" >UA-66283842-1</string> <!--per Automatically tracks screen views when the user initiates activity. The default value is False. --<bool name= "ga_autoactivitytracking" >true</bool> <!--it is tracked automatically every time an uncaught exception occurs in your app. The default value is False. --<bool name= "Ga_reportuncaughtexceptions" >true</bool> <!--the verbosity of the SDK logger. Valid values from the shortest to the most detailed are: Error, warning, info, verbose. The log level is set to warning by default. --<string name= "Ga_loglevel" >warning</string> <!--data send interval, in seconds. The default value is 30 minutes. --<integer name= "Ga_dispatchperiod" >20</integer> <!--the sampling rate to use. The default value is 100.0. can be any value between 0.0 and 100.0. --<string name= "ga_samplefrequency" >100.0</string> <!--the time (in seconds) your app can stay in the background before the end of the session. The default value is 30 seconds. Set this value to negative to deactivate Easytracker session management. --&gT <integer name= "Ga_sessiontimeout" >30</integer></resources>
5. Add Statistics Code

In activity or in application, write this:

/** * */package Com.figo.study;import Com.google.analytics.tracking.android.easytracker;import Com.google.analytics.tracking.android.mapbuilder;import Android.app.activity;import Android.os.Bundle;import Android.view.view;import android.view.view.onclicklistener;import android.widget.button;/** * @author Figo * */public    Class Gaactivity extends Baseactivity {private Button btnanalytics; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.o        Ncreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_GA);        Btnanalytics= (Button) Findviewbyid (R.ID.BTN_GA); Btnanalytics.setonclicklistener (New Onclicklistener () {@Override public void OnClick (Vi                    EW v) {try {Easytracker tracker = easytracker.getinstance (gaactivity.this); Tracker.send (Mapbuilder createevent ("Lang", "lang_downlOad "," types ", 2l). Build ());                                           } catch (Exception e) {//Todo:handle Exception}    }        });        } @Override protected void OnStart () {//TODO auto-generated Method Stub Super.onstart ();    Easytracker.getinstance (This). Activitystart (this);        } @Override protected void OnStop () {//TODO auto-generated Method Stub super.onstop ();    Easytracker.getinstance (This). Activitystop (this); }}


Ii. How to develop Android apps using Android studio how to access Google Analytics

Reference Document: Https://developers.google.com/analytics/devguides/collection/android/v4/start,androidstudio development, Create the GA account and the project as before.

The other steps are as follows:

1. Add the dependency to your project ' s top-level build.gradle:
Classpath ' Com.google.gms:google-services:1.3.0-beta1 '


2. Add the plugin to your app-level build.gradle:
Apply plugin: ' Com.google.gms.google-services '


3. Now, your need to add a dependency for Google Play Services. Inside your app ' s build.gradle add:
Compile ' com.google.android.gms:play-services-analytics:7.3.0 '

4, put on the application

Private Tracker Mtracker;      Synchronized public Tracker Getdefaulttracker () {        if (Mtracker = = null) {            Googleanalytics analytics = Googleanaly Tics.getinstance (this);            Mtracker = Analytics.newtracker (r.xml.analytics);        }        return mtracker;     }

Analytics.xml is the same as eclipse, but is placed under the XML folder

5, Other places call

       public void OnEvent (string category, String action, string label, Long value) {        try {            mtracker = mainapp.shared (). Getdefaulttracker ();            Mtracker.send (New Hitbuilders.eventbuilder ()                    . Setcategory (category)                    . Setaction (Action). SetLabel (label). SetValue (value)                    . Build ());        } catch (Exception e) {            if (E! = null) {                e.printstacktrace ();}}    }

6, Login GA View effect

https://www.google.com/analytics/web/

7. Tips

GA is generally required to be accessed via a foreign VPN. Our application is generally used for foreign users, so the foreign user access to call GA interface is no problem, domestic users need to connect to the foreign VPN when testing.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Development Step-by-step 70:android access to Google Analytics summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.