I have an application which most of the time works in the background, as a Service. There is a lot of examples and tutorials online on how you can use Google Analytics API with EasyTracker library to track multiple Activities, but there is not a single one that explains, how to use Google Analytics API in a Service. Is it even possible?
Good news! You can. and it's quite easy.
You'll need the application context let's call it mCtx
When you have this you need an instance of GoogleAnalytics, you can get it by calling
GoogleAnalytics mGaInstance = GoogleAnalytics.getInstance(mCtx);
now you need to set any parameters you want (which you would normaly put in analytics.xml when using EasyTracker).
now you need a Tracker instance:
Tracker mTracker = mGaInstance.getTracker("UA-XXXX-Y"); // your ID here
and that's basically it.. now you can send events with this tracker
mTracker.sendEvent(....);
etc..
Hope this helps. This is the very basics but GoogleAnalytics and Tracker replace the EasyTracker.
You can read more about it here:
Advanced Configuration - Android SDK
Just note that until you'll see the reports on the GA website, it can take up to 24 hours.. so be patient :) or use mGaInstance.setDebug(true) to see in the logcat that it has been sent
I would suggest not to do so unless you are very sure what you are doing.
I implemented GA events in my service but it corrupted a lot of my GA stats such as session duration, daily percentage of new sessions, daily sessions etc.
GA thinks that events are caused after a screen view and so it pushes the GA event with a screen name "(not set)".
Since services ran in the background a lot of times, it ended up corrupting the various stats.
The real-time display of active users also went wrong.
Related
I am using a simple Google Analytics code. When I look the real-time users on Google Analytics, I can see "1 active user now". There is no problem, but I am suspicious that this code will give wrong results on Google Analytics because When I look their guides, their implementations is different. Can this code give wrong result on Google Analytics?
My Code:
Declaration in MainActivity
public static GoogleAnalytics analytics;
public static Tracker tracker;
Implementation in onCreate
analytics = GoogleAnalytics.getInstance(this);
analytics.setLocalDispatchPeriod(1800);
tracker = analytics.newTracker(unitid);
tracker.enableExceptionReporting(true);
tracker.enableAdvertisingIdCollection(true);
tracker.enableAutoActivityTracking(true);
No more code, just 6 lines.
If there is no problem for this code, why their guide is complex?
Note: There is no problem now, but I am suspicious about future.
This is, indeed, close to the simplest implementation possible.
The problems with it, however, is that:
GA library has hardcoded triggers that are qualified to be "screenviews". While it may be sufficient for very simple apps, it poorly handles layered views logic that is commonly used in more complex apps.
GA library has to send screen identifiers. And there's little to none of what it can use for that. So what it does is it tries to pull class names as identifiers for screen names when no explicit name is set (which is the case in automated tracking). This results in the data being hard to read.
Personally I've never seen auto activity tracking being used in production. Maybe it makes sense to be used by the developers to improve exceptions logging in Firebase, but the data is way too inconsistent and hard to decode to be flexibly use for business reporting.
You achieve a lot better tracking quality when you set each screenname explicitly. That is the best practice.
I want to do data analytics for my android application using Piwik.
In Piwik's documentation, they suggest using GitHub.
I got the Piwik API and download the following file:
https://github.com/piwik/piwik-java-tracking
which I don't know how to use.
What are the steps needed to get the data analysis using the Piwik working?
What should I do next?
I also would like to implement piwik tracking in my android app.
This is what I understood so far:
Unlike for iOS/MacOS applications, there is currently no SDK to help us out.
Piwik developers have a ticket opened in their bugtracker about this, which shows they're aware of the issue and willing to improve the situation, but it will take some time before this android tracking SDK is released.
At the moment, the Piwik team encourages android and Java developers to use the piwik-java-tracking library you mentioned. This is basically just a java wrapper for the web tracking API reference, which helps you to generate Tracking Request URLs to send to your Piwik instance.
This piwik-java-tracking project lacks documentation (there is none that I know of in Github), but there is javadoc in the java files.
Basically what you need to do to track an action is to:
Create a new instance of SimplePiwikTracker
Feed it with whatever values and parameters you wish to track using the various setters available
When you're done, get the URL using one of the methods defined in the PiwikTracker interface, depending of the type of "event" you want to track
Send a request to that URL to actually track your action into you Piwik instance.
This has several drawbacks:
If you need bulk tracking, you'll have to figure out a way of doing so. The web tracking API reference states you can use use a HTTP POST request containing a JSON object to do it.
It doesn't provide any help to handle the case where user is offline. If you need to, you'll have to find a way to cache your tracking request (in an sqlite database for example) and bulk send them when the user is back online.
You'll need to handle possible exceptions raised by network error for the tracking request, to make tat tracking does not interfere with your app normal behavior.
Be sure to read this article too. It gives you an overview of what you need to do to get up & running.
This is what I understood so far. I may update this answer as I progress in implementing piwik tracking in my own app.
Good luck.
Edit: I just noticed that Piwik released "Piwik SDK Android". There are some instruction on how to get this working in the project's wiki.
I have an application with a reused activity (its used for different purposes so just tracking the activity name would not help me, thats why I want to define the screen name manually while still using the EasyTracker. My code looks like this:
EasyTracker t = EasyTracker.getInstance(this);
t.set(Fields.SCREEN_NAME, screenName);
// MapBuilder map = MapBuilder.createAppView();
// map.set(Fields.SCREEN_NAME, screenName);
// t.send(map.build());
t.activityStart(this);
I got these information from https://developers.google.com/analytics/devguides/collection/android/v3/screens
I dont want to do this without the EasyTracker because I didn't find any information what exactly happens in the methods activityStart() and activityStop() of the easy tracker so I cant reproduce this behaviour with a custom tracker and I think the inforation which is collected in the activityStart() method in addition to the screen name is also very usefull. So has someone experience with defining custom values in the easy tracker? Is it a good way to do it this way or is there a better solution?
If anyone comes across the question, the as per the v3 of the Google Analytics SDK,
EasyTracker.getInstance(this).activityStart(SCREEN_NAME); // Add this method.
where SCREEN_NAME is the user defined constant for the reused Activity.
Using custome screen names instead of the default ones i.e the full qualified path name of the Activity
doesn't cause any issues in data collection.
Recommendation, Use Google Tag Manager for your Tracking purpose, it provide you better flexibility like changing the UA property ID in future as well the screen names to be sent to Google Analytics. Read more # Developer Guide For Android
I'm trying to add more data to my Google Analytics report regarding the use of my Android app.
I started using custom dimensions since yesterday and didn't get any results yet and I want to make sure I'm doing it right.
I want to tie a page view with additional information to be sent with it, and that's why I'm using custom dimensions. In the onCreate method of a specific activity I'm doing this -
Tracker tracker = EasyTracker.getTracker();
tracker.setCustomDimension(1, 128));
tracker.setCustomDimension(2, 2));
tracker.setCustomDimension(3, 45));
EasyTracker.getTracker().trackView();
I've set my custom report to work by the Screen Views metric, and by one of the dimension Id's from above.
Will all of this data be sent along with the trackView command? Because I still can't see any data from yesterday.
Thanks!
It works. This time I needed to wait about 2 days for the data to appear.
Now I need to figure out how to show the data from all these dimensions at once, but I guess this fits another question.
I don't have any experience with GA tracking using the android sdk api but I did find this page in the GA documentation. In the code examples they use the following as the trigger:
tracker.sendView();
So I'm still mucking around with this Google Analytics SDK. According to this, I should be able to send custom vars with a visitor scope that include the app version, and the phone model.
Using tracker.getVisitorCustomVar(i) I can see that the information is indeed stored, and the log shows NetWorkRequestUtil/ConstructPageviewRequestPath with a message that contains the custom vars.
But it does not show up in the analytics report along with the other tracking information. I thought I would find it under Visitors->Custom Variables, but that only says
There is no data for this view.
Where can I see the custom variables? :(
How long ago did you incorporate the custom vars? Analytics is not realtime, there can be an --i think-- 24 hour gap?
I'm doing this, and it works for me. I'm assuming you do get other data, so your initialisation and 'dispatching' of the tracker is correct?
You can set your customvar like so (as I imagine you are doing allready).
tracker.setCustomVar(TRACK_SLOT_AUTH, TRACK_CVAR_AUTH, isAuth, TRACK_SCOPE_SESSION);
(using global vars to be sure not to override already used slots here, but the intention should be clear)
The data shows up under Visitors->Custom Variables
Custom variables have been replaced by Custom Dimensions and Metrics.
https://developers.google.com/analytics/devguides/collection/android/v2/migration#update-methods