Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 207
Evgeni Shafran Avatar asked Jul 01 '26 01:07

Evgeni Shafran


1 Answers

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(....);
like image 75
Evgeni Shafran Avatar answered Jul 03 '26 14:07

Evgeni Shafran