Evgeni Shafran
Evgeni Shafran

Reputation: 146

Using Google Analytics v2 without Activity / EasyTracker

When using EasyTracker:

@Override
    protected void onStart() {
        super.onStart();

        EasyTracker.getInstance().activityStart(this);
    }

It work great, the problem that i am integrating from older version of analytics and i use it in a service and not in activity, so i cant use activityStart method.

I tried to use:

GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(getApplicationContext());
final Tracker tracker = googleAnalytics.getTracker("UA-xxxxxx-y");
tracker.setStartSession(true);

tracker.sendView("/page");

And i dont see anything in the analytics (even after GAServiceManager.getInstance().dispatch())....

Is there any way to use new version of analytics whitout the activity???

Thanks

Upvotes: 3

Views: 2383

Answers (2)

Evgeni Shafran
Evgeni Shafran

Reputation: 146

Found a way to not use EasyTracker. It was actually in the oficial site: https://developers.google.com/analytics/devguides/collection/android/v2/advanced

Basically this what you need to do: At first initial the tracker like this:

// Get the GoogleAnalytics singleton.
mGaInstance = GoogleAnalytics.getInstance(this);

// Use the GoogleAnalytics singleton to get two Trackers with
// unique property IDs.
mGaTracker = mGaInstance.getTracker("UA-XXXX-Y");

Then you can get the tracker like this:

mGoogleAnalytics.getDefaultTracker();

And use it like:

mGoogleAnalytics.sendEvent(.....);

mGaTracker.sendView(....);

Upvotes: 1

Raanan
Raanan

Reputation: 4775

In a service you need to set the Context before sending a view

Try this:

EasyTracker.getInstance().setContext(this);
EasyTracker.getTracker().sendView("/page");

Upvotes: 0

Related Questions