Using Google Analytics v2 without Activity / EasyTracker - android

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

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(....);

In a service you need to set the Context before sending a view
Try this:
EasyTracker.getInstance().setContext(this);
EasyTracker.getTracker().sendView("/page");

Related

Debug Android Analytics event tracking - Analytics not picking up events

I've successfully added Analytics to an Android app and it is picking up session data just fine. However, I've also tried to track a specific event, but it does not seem to be sent to Analytics like the rest of the session data. I've followed the code suggested in the current Analytics documentation and still no luck, so I'm pasting here in the hopes that it's something obvious to more experienced folks. Many thanks in advance for any guidance.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_import);
user = getIntent().getStringExtra("user");
statusUpdateArea = (EditText)findViewById(R.id.dataStatusUpdateField);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
welcomeMessage = (TextView)findViewById(R.id.WelcomeMessageTextView);
welcomeMessage.append(user);
// getting tracker & setting User ID field
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
mTracker.set("&uid", user);
// build and send event
mTracker.send(new HitBuilders.EventBuilder()
.setCategory("User ID")
.setAction("Data login")
.setLabel(user)
.build());
...
maybe you need use
mTracker = application.getTracker();
and
mTracker.setScreenName("yourscreenname");
Seems you are calling send before calling build .
Create a tracker with all the event info then call build and once done call send function .
Have worked on webtrends analytics product so I know a thing or two about analytics

Google Analytics Android Real Time

I'm trying to implement Analytics on my app. Actually I did it, but I'm not able to see interactivity in real time, it's takes some time, like 2~3 minutes to appear something. I tried to set Dispatch Period but it didn't take effect.
I followed the implementation of Google Analytics Developer, some code below:
public class AppController extends Application {
public enum TrackerName {
APP_TRACKER // Tracker used only in this app.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = analytics.newTracker(R.xml.global_tracker);
analytics.setLocalDispatchPeriod(2);
// Set the log level to verbose.
GoogleAnalytics.getInstance(this).getLogger()
.setLogLevel(LogLevel.VERBOSE);
// Tracker t = analytics.newTracker(R.xml.global_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
The code to send:
private static void SendView(Activity act, String view) {
// Get tracker.
Tracker t = ((AppController) act.getApplication())
.getTracker(TrackerName.APP_TRACKER);
// Set screen name.
// Where path is a String representing the screen name.
t.setScreenName(BuildName(view));
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
}
Thanks in advance.
Short answer: you can't do immediate dispatch with Google Play Services.
This is done to save traffic.
On devices with Google Play Services installed, by default, auto dispatching with 2 minute interval is enabled and manual dispatching is not available.
https://developers.google.com/analytics/devguides/collection/android/v4/dispatch
What it doesn't say is that these 2 minutes can only be increased programmatically. iOS has a similar system, but the time frame is a bit different. Either 1 minute or 30 seconds.
Generally, you don't want to look at the real time GA data to debug your
tracking implementation. Have a wrapper around your GA/Firebase library and have logging in that wrapper.
Additioanlly, the Real time data reports in GA are severely lacking dimensions and aggregation options. You want to wait for a day or two for the data to be made properly available.

Simple case of using Google Analytics for android doesn't works

I'm having a really hard times using Google Analytics in my android application.
I don't need anything complicated, all I want to do is to receive general information about my users, and to receive reports whenever the application crushes.
I performed the following steps:
I created an Application class which contains a private Tracker object.
inside the onCreate function I initialized it.
public class MyApplication extends Application{
private Tracker googleAnalyticsTracker;
#Override
public void onCreate() {
super.onCreate();
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
googleAnalyticsTracker = analytics.newTracker(R.xml.google_analytics_tracker);
googleAnalyticsTracker.enableAutoActivityTracking(true);
googleAnalyticsTracker.enableExceptionReporting(true);
}
}
I created an xml file in order to generate the tracker (google_analytics_tracker.xml), as you can see here:
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_reportUncaughtExceptions">true</bool>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-*THE ID WHICH I RECIVED*</string>
nevertheless - whenever I execute the application I can't see anything in my google analytics dashboard. Whenever the program crushes - I can't see anything in the google analytics Behavior/'crashes and exceptions' as wel.(I'm looking
does someone have a clue what could be the problem?
Thanks a lot
Try the instructions on this page. You are missing a few things such as sending a screen view event as well as any activity code to getting the global tracker
https://developers.google.com/analytics/devguides/collection/android/v4/

Android: Google Analytics availability in Google Play Services?

Google Analytics has been announced to become part of the rolling out Google Play Services 4.3, however it is not yet included in the Google Play Services packages list:
http://developer.android.com/reference/gms-packages.html
Any idea when it will become available, and will it be safe to be used straight away, or will it be better to wait for some time to make sure every user has Google Play Services 4.3 already installed?
I've noticed some other differences.
Tracker
To get a new Tracker, use the newTracker() method (accepts both a String value and an int value [for XML configuration]):
googleTracker = gaInstance.getTracker(GA_KEY); // OLD
googleTracker = gaInstance.newTracker(GA_KEY); // NEW
EasyTracker
EasyTracker has now disappeared, so we will have to use GoogleAnalytics.getInstance(this).reportActivityStart(this) as reported by Paito.
Setters
The googleTracker.set() method is no longer available. It has been replaced with more specialised methods, for example:
googleTracker.set(Fields.SCREEN_NAME, null); // OLD
googleTracker.setScreenName(null); // NEW
Event creation
The googleTracker.send() method has also seen some changes.
googleTracker.send(MapBuilder
.createEvent(category, action, label, value)
.build()); // OLD
googleTracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.setValue(value)
.build()); // NEW
AppView
It now becomes
googleTracker.send(MapBuilder.createAppView().build()); // OLD
googleTracker.send(new HitBuilders.AppViewBuilder().build()); // NEW
AppViewBuilder
AppViewBuilder has now been deprecated, replaced by the new ScreenViewBuilder class. (thanks Hai Phong for the tip!)
For those who are running into (or have already dealt with) the Dalvik's 64K methods limit, there are now 3K methods that you will be able to get rid of in your application, thanks to this integration.
It's part of the package list now.
I think the basic functionality works something like this...
import com.google.android.gms.analytics.GoogleAnalytics;
#Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
#Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}
As per conversation in order to use Easytracker replacement with
GoogleAnalytics.getInstance(this).reportActivityStart(this);
GoogleAnalytics.getInstance(this).reportActivityStop(this);
You need to add your config to AndroidManifest like
<meta-data android:name="com.google.android.gms.analytics.globalConfigResource" android:resource="#xml/analytics_global_config" />
I'm still having to get instance of Tracker to send Events, may be somebody else would have better luck at replacing
EasyTracker.getInstance(mContext).send(MapBuilder....)
The documentation for Google Analytics SDK v4 (now part of Google Play Services) has just been published!
https://developers.google.com/analytics/devguides/collection/android/v4/

Realtime android Google analytics without EasyTracker

I'm trying to figure out how to track page views in real time without using the EasyTracker singleton.
I have the following code:
/**
* This how I create the tracker instance...
*/
private void createTracker() {
if (tracker != null)
return;
tracker = googleAnalytics.getTracker(googleAnalyticsSiteId);
tracker.setAnonymizeIp(trackingLevel.isAnonymous());
tracker.setAppInstallerId(configuration.getInstallationUUID());
tracker.setSampleRate(dispatchIntervalInSeconds);
trackUpgradedApp();
}
...
void someOtherMethod(Activity activity){
tracker.sendView((String) activity.getTitle());
// EasyTracker.getInstance().activityStart(cyborgScreen.getActivity());
}
While I uncomment the EasyTracker line, I can track the pages visited in realtime, but only after adding the analytics.xml to the project (obviously).
Once commenting the line, I do not receive the realtime information...
If following the code here, it should have done the trick.
Am I missing the obvious, or there is a bug?
Thanks,
Adam.
Well, after tones of playing around deleting, coping, changing things, I've found that this:
GAServiceManager.getInstance().setDispatchPeriod(dispatchIntervalInSeconds);
was missing, and that is why I could not see the realtime screen event without using the EasyTracker!
Hope this helps someone...
The answer is you call:
tracker.sendView((String) activity.getTitle());
but it can take time until you see it!

Categories

Resources