As described in your link, the GA-related code should be placed inside the Application
class instead of the Activity
.
Create a class extends Application
like this:
public class MyApplication extends Application{
//the tracker related code should be place here (the enum, getTracker, mTrackers)
}
then in your AndroidManifest.xml
, under the application
tag, replace the android:name
with the above MyApplication
class.
The tracker code should be placed in the Activity.oncreate
method such as
public class MainActivity extends Activity {
.
.
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Set screen name.
// Where path is a String representing the screen name.
t.setScreenName(path);
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
}
.
.
.
}