简体   繁体   中英

Google Analytics, the best way to implement the code

I'm developing an Android application, i would implement Google Analytics to track my application.

actually i use this code:

 @Override
 public void onStart() {
  super.onStart();
  Tracker tracker = GoogleAnalytics.getInstance(this).getTracker("UA-xxxxxxx-1");
  tracker.set(Fields.SCREEN_NAME, "SCREEN NAME");
  tracker.send(MapBuilder.createAppView().set(Fields.customDimension(1), "Premium").build());
 }

I would know some information:

1) Is it or not wrong implements this code ? Do i must add something in OnDestroy(); function ?

2) i have 5 Activity and i want track it all and i want see in my Google Analytics account the differents screens that user seen during the using of my application, the code that i posted is it correct ?

Create an xml file under res/values/ called analytics.xml

And organize this way:

<?xml version="1.0" encoding="utf-8" ?>

<resources>

<!--Replace placeholder ID with your tracking ID-->
<string name="ga_trackingId">UA-xxxxxxx-x</string>

<!--Enable automatic activity tracking-->
<bool name="ga_autoActivityTracking">true</bool>

<!--Enable automatic exception tracking-->
<bool name="ga_reportUncaughtExceptions">true</bool>

<string name="com.example.project.Class">Main Activity</string>
<string name="com.example.project.Class2">Activity 2</string>

</resources>

And in each Activity class, insert this before you close last bracket:

//Tracker methods...

    @Override
      public void onStart() {
        super.onStart();
        // The rest of your onStart() code.


        EasyTracker.getInstance(this).activityStart(this);  // Add this method.
      }

      @Override
      public void onStop() {
        super.onStop();
        // The rest of your onStop() code.
        EasyTracker.getInstance(this).activityStop(this);  // Add this method.
      }

      //Tracker method end...

Note: Imports for tacking will be automatically added. But make sure you have already settled with Google Service and libraries into your project before doing this.

Download the Google Play Services SDK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM