ephramd
ephramd

Reputation: 380

Google Analytics version 3 - How to apply it correctly?

I've added google analytics to my app with the intention of obtaining information about the screens you and send custom events.

I am obtained duplicate content ... Also I get different results: "com.package.app.MainScreen" - 300 views and "Main Screen" - 200 views

I am interested to get only follow up with the custom name of the activity and not the package. And in any case, because both show different results?

public class MainScreen extends Activity {
private static final String GA_PROPERTY_ID = "UA-12345678-9"; 
private static final String SCREEN_LABEL = "Main Screen";
Tracker mTracker;
EasyTracker easyTracker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);

    mTracker = GoogleAnalytics.getInstance(this).getTracker(GA_PROPERTY_ID);
    mTracker.set(Fields.SCREEN_NAME, SCREEN_LABEL); // For Custom Name from activity
    mTracker.send(MapBuilder.createAppView().build());

    easyTracker = EasyTracker.getInstance(this); //  Analytics Events
    ...
    easyTracker.send(MapBuilder.createEvent("MainScreen", "Play", category.get(1), null).build()); //AnalyticsEvents
    ...


}

@Override
public void onStart() {
    super.onStart();
    EasyTracker.getInstance(this).activityStart(this);
}

@Override
public void onStop() {
    super.onStop();
    EasyTracker.getInstance(this).activityStop(this);
}

}

And analytics.xml:

<?xml version="1.0" encoding="utf-8" ?>
    <resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
      <!--Replace placeholder ID with your tracking ID-->
      <string name="ga_trackingId">UA-12345678-9</string>
      <!--Enable automatic activity tracking-->
      <bool name="ga_autoActivityTracking">true</bool>
      <!--Enable automatic exception tracking-->
      <bool name="ga_reportUncaughtExceptions">true</bool>
    </resources>

Google Analytics Dev Guide

Upvotes: 2

Views: 1891

Answers (1)

Tonithy
Tonithy

Reputation: 2290

You have 3 separate tracking instances, 2 in the onCreate() and also one in the onStart()/onStop(). I would recommend removing both of them from the onCreate() and just keeping the EasyTracker in the onStart()/noNstop() that way you won't get redundant data, but still get session lengths.

To have custom names for the EasyTracker tracked Activities, you can set them in your analytics.xml by adding the following fields as such:

<-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<-- The screen names that will appear in reports -->
<string name="com.example.app.BaseActivity">Home</string>
<string name="com.example.app.PrefsActivity">Preferences</string>

Here is an onClick(View) with the EasyTracker to send up UI events:

@Override
public void onClick(View view) {
    String action = "";
    switch(view.getId()) {
    case R.id.btnWeb:
        getSherlockActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(sponsor.getWebsite())));
        action = "Website";
        break;
    case R.id.btnPhone:
        getSherlockActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + PhoneNumberUtils.stripSeparators(sponsor.getPhone()))));
        action = "Phone";
        break;
    }

      EasyTracker easyTracker = EasyTracker.getInstance(getSherlockActivity());
      if (easyTracker != null) {
          easyTracker.send(MapBuilder
              .createEvent("ui_action",
                           "sponsor_page",
                           title + ": " + sponsor.getName() + " - " + action,
                           null)
              .build()
          );
      }
}

Upvotes: 1

Related Questions