Android: Google Analytics availability in Google Play Services? - android

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/

Related

Google play game Leaderboard can not submitScore

I am trying to submit a score at a Google Game Play Leaderboard using the following code line:
if(mGoogleApiClient.isConnected()) {
Games.Leaderboards.submitScoreImmediate(mGoogleApiClient, "xxxxxxxxxxxxxxxxx", myScore).setResultCallback(new ResultCallback<Leaderboards.SubmitScoreResult>() {
#Override
public void onResult(Leaderboards.SubmitScoreResult arg0) {
Log.e(TAG, "getStatusCode= "+arg0.getStatus().getStatusCode());
Log.e(TAG, "score submitted: "+arg0.getScoreData().toString());
}
});
}
The score is not uploaded and I get the following error ScoreSubmissionData:
ScoreSubmissionData{PlayerId=xxxxxxxxxxxxxxxx, StatusCode=2, TimesSpan=DAILY, Result=null, TimesSpan=WEEKLY, Result=null, TimesSpan=ALL_TIME, Result=null}
StatusCode=2 means STATUS_CLIENT_RECONNECT_REQUIRED
I call the above line inside onConnected() method so it is sure that I am connected...
Try reconnecting or apply a Loop-switch sequence to handle error like this. You might want to check this example showing how to use STATUS_CLIENT_RECONNECT_REQUIRED of the com.google.android.gms.games.GamesClient class.
Base on the document:
STATUS_CLIENT_RECONNECT_REQUIRED
The AppStateClient is in an inconsistent state and must reconnect to the service to resolve the issue.
Also here is the list of AppStateClient that you may encounter.
Hope this helps.
I have found the solution.
The application i make was first build to an other Laptop.
Now i have bought a new Laptop, i installed Android Studio and tried to change some code lines. But when i submitted a Score my Google play account (API Manager) could not recognize my new Laptop because it had a different SHA1 key.
So i added my new SHA1 key at the credentials of the API Manager and everything was ok !!

How to use the DriveApi.OnSyncFinishCallback (Android's Google Play Services)

The Android's developer documentation states that you can use a DriveApi.OnSyncFinishCallback to (presumably) handle when a synchronization between your local contet and your google drive account is completed. Normally such synchronization appens automatically, trasparently managed by Google Play Services, but apparently you can force a sync request with a call to:
Drive.DriveApi.requestSync(mGoogleApiClient);
I say "apparently" because the official documentation of this function is very poor, at least (https://developer.android.com/reference/com/google/android/gms/drive/DriveApi.html#requestSync(com.google.android.gms.common.api.GoogleApiClient))
Anyway, a OnSyncFinishCallback can be instantiated with this code:
OnSyncFinishCallback myCallback = new OnSyncFinishCallback(){
#Override
public void onSyncFinish(com.google.android.gms.common.api.Status arg0) {
// TODO Auto-generated method stub
}
};
My question is where and how can I register this callback so it will be called automatically when the sync is completed? The requestSync call returns a PendingResult that only have a setResultCallback(ResultCallback arg0) method, that can't be used for a OnSyncFinishCallback.
I must say that requestSync is working absolutely fine for me (January 2015, with Google Play Services 6.5.87). I do a backup of my database on one device and restore it on another device, but before the restore I call requestSync this way:
Drive.DriveApi.requestSync(mGoogleApiClient)
.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status result) {
if (!result.isSuccess()) {
// Sync not ok
showMessage("Sync error");
return;
}
// Sync ok. I can safely do a query to get
// the database file and restore it.
...
By the way, I'm using the root folder, not the appfolder. The appfolder might have additional synchronization issues when installing/uninstalling the app from different devices, so for the moment I prefer to stick with root folder.
OnSyncFinishCallback is a red herring, it shouldn't be exposed.
Just add a callback handler to requestSync like any other GoogleApiClient method:
Drive.Drive.requestSync(mGoogleApiClient).setResultCallback(
new ResultCallback<Success>() {
//...
});
It turned out that OnSyncFinishCallback was removed from API and DriveAPI.requestSync() doesn't do what it's supposed to. Fortunately Google just introduced new Drive API for Android in version 6.1 of Google Play Services, in particular the Completion Events, that makes exactly what OnSyncFinishCallback was supposed to do. More official detail here https://developers.google.com/drive/android/completion

Google Analytics for tracking search values in android

In my android application, I have a search functionality. I would like to track the search keywords that my users search for. As far as I know, Google Analaytics could track events in my android app. I was wondering if it could do the same for search items.
If it is possible, a sample code or a link would be great!
That's mostly a question of how to segment your data in the Analytics web app, almost a science in itself.
Example:
EasyTracker.getTracker().sendEvent("Search", "Keywords", searchString, 1L);
Remember the basic usage of EasyTracker:
https://developers.google.com/analytics/devguides/collection/android/v2/
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("UA-YOUR-ACCOUNT-HERE", this);
After defining tracker, you should call the below method.
public static void trackEventGoogle(String category,String action,String label,int value)
{
try
{
GoogleAnalyticsTracker.getInstance().trackEvent(category,action,label,value);
}
catch (Exception e) {
exceptionLog(e);
}
}
I hope it helps. Don't forget to add analytics jar file to your project.

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!

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
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");

Categories

Resources