From Application onCreate(), to test event/screen logging:
public static void logUserAction(String eventName) {
Timber.i("Logging to Google Analytics");
Tracker t = getTracker();
t.setScreenName(eventName);
t.send(new HitBuilders.ScreenViewBuilder().build());
t.send(new HitBuilders.EventBuilder()
.setCategory("Event")
.setAction(eventName)
.setLabel(eventName)
.setValue(1)
.build());
}
synchronized static Tracker getTracker() {
if (googleAnalyticsTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(application);
googleAnalyticsTracker = analytics.newTracker(R.xml.global_tracker);
}
return googleAnalyticsTracker;
}
Config file, global_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<string name="ga_trackingId">UA-ACTUAL_VALUE_USED_HERE</string>
</resources>
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Build file
compile 'com.google.android.gms:play-services-analytics:7.0.0'
The UA tracking code I'm using is definitely correct. The project builds and executes, however although the log message is shown to confirm the code is called, absolutely nothing happens in debug or release builds. No data is showing in Analytics, and there is nothing in Logcat to guide me.
What did I miss?
Like my comment said, you have to wait some times after the "Google Analytics ID" creation, before you can view any reports.
Related
I need to use the drive api to create a file with some content from my app, so I followed the "Getting Started" section from Drive api web page.
So I enabled the api on my developer´s console, created an OAuth client id as it says. (Can I use the api if I haven´t paid the 2$5 yet?)
On my settings activity on the onCreate method I'm doing:
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
I'm implementing the methods:
#Override
protected void onStop(){
getGoogleApiClient().disconnect();
super.onStop();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(STATE_RESOLVING_ERROR, resolvingError);
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (resolvingError) {
// Already attempting to resolve an error.
return;
}else if (connectionResult.hasResolution()) {
try {
resolvingError = true;
connectionResult.startResolutionForResult(this,RESOLVE_CONNECTION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
getGoogleApiClient().connect();
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
resolvingError = true;
}
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if(requestCode == RESOLVE_CONNECTION_REQUEST_CODE) {
resolvingError = false;
if (resultCode == RESULT_OK) {
if(!getGoogleApiClient().isConnecting() && !getGoogleApiClient().isConnected()){
getGoogleApiClient().connect();
}
}
}
}
#Override
public void onConnected(Bundle bundle) {
Toast.makeText(this, "Success!!! :D :D", Toast.LENGTH_SHORT).show();
}
#Override
public void onConnectionSuspended(int i) {}
Then I call googleApiClient.connect(); on a button onclick method.
I'm getting the select account google drive dialog, select an account and press OK, and I'm getting this error on the connection result:
E/GooglePlayServicesUtil﹕ The specified account could not be signed in.
I searched over the web and cant find the cause of this problem.... maybe I'm doing something wrong on the developers's console... don't know...
UPDATE:
So i delete the project on Google developer´s console and create it again, i also add the Drive Api and enable it, i added a product name on authorization screen, and a new client id with my package name and sha1 id...
also update my manifest,
added to manifest tag:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
to application tag:
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
to the activity tag where im doing the connection:
<meta-data
android:name="com.google.android.apps.drive.APP_ID"
android:value="id=<app id i took this id from developers´console>" />
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
<data android:mimeType="application/vnd.google-apps.drive-sdk.<app id>" />
i also building a signed apk using the debug keystore to test my app. (on real device).
after all this the error is still rising...
My guess is that there is a problem with the setup you did at the Developer's Console. This discussion may be helpful. – qbix
hello, reading the discussion qbix mention above i just found the cause of my problem...
the default flavor at my project structure had an incorrect Application id, i just change it to my app package "com.xxxxx.yyyyy".
so i beleave when googleApiClient attempted to connect, find diferent package name at id client credencials and deny access....
now is working fine thanx....
Here's my usual checklist:
1/ get the APK in question, push it through an unzipper, like 7-zip. you'll find a folder META-INF with a file CERT.RSA. Get it.
2/ run 'keytool -printcert -file ......\CERT.RSA' and copy/save the SHA1 '11.22.33...99'
3/ get you package name (from manifest?) 'com.blabla.yourproject'
4/ open developers console 'https://console.developers.google.com/project'
5/ in your project, check:
Enabled APIs: drive API
Credentials: Package name: 'com.blabla.yourproject', Fingerprint: '11.22.33...99'
Consent Screen: Email: yourmail#gmail.com , Product Name: 'YourProject'
Good Luck
Pleas try to First normal Google account Login
mGoogleApiClient = new GoogleApiClient.Builder(this).
addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
//.addApi(Drive.API)
//.addScope(Drive.SCOPE_FILE)
.build();
In my case cause was in incorrect 'package name' in OATH client settings:
was: com.blabla.prog
correct one: bla.bla.com.prog
try to ignore the API connection by doing this:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApiIfAvailable(Drive.API)
I've done in this way and everything seems working. I really don't know why, i suppose that the Driver.API creates some problems, and doing in this way you can use the maps,the markers and all that stuff. I know that is not a good answer because i don't provide you a correct explanation, but anyway, it can help.
This type of error may occur in following situations:
If you have used release key SHA1 signing-certificate fingerprint while creating Oauth 2.0 client ID and running your application without signing it with release key (i.e. running with debug key). This is also true for debug key.
If you have used different package name while creating Oauth 2.0 client ID
Another thing that can go wrong is that you create credentials for "API Key" instead of "OAuth 2.0 client ID".
Yeah silly I know.. guide says "Client ID" so I guess I went by the length of the string :)
Ok, I have been all over looking for an answer as to why my Google Play campaign measurement is not working. I am not getting any data in my Analytics account. Here is what I have:
Google Play Services version 6171000 imported into my project and added as a reference.
Google Play App version 5.0.38 on my testing device
In my res\xml\global_tracker.xml file I have:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="https://schemas.android.com/tools" tools:ignore="TypographyDashes">
<integer name="ga_sessionTimeout">300</integer>
<string name="ga_trackingId">MY-TRACKING-ID</string>
<bool name="ga_debug">true</bool>
<!-- Enable automatic activity tracking -->
<bool name="ga_autoActivityTracking">false</bool>
<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
In my Manifest I have:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
and within <application> I have:
<!-- Used for Google Play Store Campaign Measurement -->
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver
android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="#xml/global_tracker" />
Now, I did the getTracker method in my extended Application class suggested by the Analytics setup document, but I don't think that is needed for just the app download tracking. Please correct me if I'm wrong here and I will post the code I have for that as well.
To test I have tried running
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.my.package/com.google.android.gms.analytics.CampaignTrackingReceiver --es "referrer" "utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=testContent&utm_campaign=testCampaign"
and I get a "Broadcast completed: result=0"
and then open my app on my device and I get nothing in the logs about campaign. Although I did read that this is not the proper way to test when using V4.
I've also packaged my app and uploaded to the Beta section of my store listing. While logged into one of my beta tester accounts, I opened the following in my browser and downloaded the app.
https://play.google.com/store/apps/details?id=com.my.package&referrer=utm_source%3DTestSite%26utm_medium%3DsideBanner%26utm_term%3DTestTerm%26utm_content%3DTestContent%26utm_campaign%3DtestCampaign
This I would expect to yield some sort of results in the Acquisition->Google Play Referral section of my Analytics account and have something to do with testCampaign. I do not see anything in my account not even after 24 hours (not sure that's still required but I remember it used to be for GA).
If anyone knows of a piece that I'm missing or why it would not be showing up in my account, please point me in the right direction. Thank you.
EDIT:
Here is my tracker code in my extended Application Class:
private static final String TRACKER_TAG = "GA Tracker";
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public enum TrackerName {
APP_TRACKER // Tracker used only in this app.
}
synchronized Tracker getTracker(TrackerName trackerEnum) {
Log.d(TRACKER_TAG, "Getting GA tracker");
if (!mTrackers.containsKey(trackerEnum)) {
Log.d(TRACKER_TAG, "Creating new GA tracker");
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
analytics.getLogger().setLogLevel(LogLevel.VERBOSE);
Tracker t = analytics.newTracker(R.xml.global_tracker);
mTrackers.put(trackerEnum, t);
}
return mTrackers.get(trackerEnum);
}
Maybe you already did it but,
did you set your Tracking ID at >>>>> <string name="ga_trackingId">MY-TRACKING-ID</string> ?
I am using Google Analytics v4 in my app.I published my app now I can see users in real time analytics but other reports are empty.What can be cause this ? I started using analytics 2 days ago still there is no report.But as I said I can see users in real time analyics.
This is my tracker:
package com.impact.ribony;
import java.util.HashMap;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class Trackers extends Application
{
public enum TrackerName
{
APP_TRACKER, // Tracker used only in this
// app.
GLOBAL_TRACKER, // Tracker used by all the
// apps from a company.
// eg: roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all
// ecommerce
// transactions from a
// company.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public synchronized Tracker getTracker(TrackerName trackerId)
{
if (!mTrackers.containsKey(trackerId))
{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
if( trackerId == TrackerName.GLOBAL_TRACKER )
{
mTrackers.put(trackerId, analytics.newTracker(R.xml.global_tracker));
}
}
return mTrackers.get(trackerId);
}
}
This is global_tracker.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_reportUncaughtExceptions">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="com.impact.ribony.MainActivity">MainActivity</screenName>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-42075172-2</string>
</resources>
And I am using following lines in onCreate() method:
Tracker t = ((Trackers) getApplication()).getTracker(Trackers.TrackerName.GLOBAL_TRACKER);
t.setScreenName("Main");
t.enableAdvertisingIdCollection(true);
t.send(new HitBuilders.AppViewBuilder().build());
And this one in onResume() method:
GoogleAnalytics.getInstance(this).reportActivityStart(this);
If the question is about why there is no crash reports, than it is a known bug, maybe it is someway linked with your problem, more here In general exceptions are not dispatched even with this line in V4-
<bool name="ga_reportUncaughtExceptions">true</bool>
Edit. Maybe change to ScreenViewBuilder, because AppViewBuilder is deprecated.
https://developer.android.com/reference/com/google/android/gms/analytics/HitBuilders.AppViewBuilder.html
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/
i have setup every thing in my app for using google analytics V4
and i get every things working and i can see it but when i go to real time overview in my mobile view i didn't see any active user
this is my tracker
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes">
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="info.lifepast.MainActivity">MainActivity</screenName>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-xxx-3</string>
</resources>
and the application class is
public class Analytics extends Application {
private static final String PROPERTY_ID = "UA-xxxxx-3";
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
and in my main activity on create i added this
Tracker t = ((Analytics) this.getApplication()).getTracker(
TrackerName.GLOBAL_TRACKER);
GoogleAnalytics.getInstance(this).getLogger().setLogLevel(LogLevel.VERBOSE);
// Set screen name.
// Where path is a String representing the screen name.
t.setScreenName(getString(R.string.app_name));
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
and the manifest file
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="#xml/global_tracker"/>
any help?
I've been looking at the v4 analytics today, and have also had trouble getting screen views to post. Here are a couple things I've dug up during my investigations that may be helpful for you:
AppViewBuilder is deprecated in favor of ScreenViewBuilder (see the HitBuilders source code). This part of the documentation is, presumably, out of date. Edit Mar 6, 2015: it would appear that the linked documentation has now been updated to use ScreenViewBuilder.
If my interpretation of the documentation is correct, it should not be necessary to explicitly post screen views using a ScreenViewBuilder when the auto activity tracking feature is enabled (which I see is the case in your configuration file).
By default, the current date is not included in your Google Analytics stats. You can choose to include it by manually selecting a date range (see drop-down control at the top right of most GA pages).
Make sure you shorten the dispatch period for debug builds - by default, events are batched and sent every 30 minutes, but for testing it's ok to reduce this to a few seconds. See the answer from #vangoz for implementation details.
Hope some of that helps you.
Edit: related, but I see you've already posted there: Google Analytics API v4 for Android Does NOT Send Screen Views
For me it turns out Google Analytics only dispatch the data every 30 minutes by default. So changing the dispatch time for testing show the realtime data with some delay.
GoogleAnalytics.getInstance(this).setLocalDispatchPeriod(15);
Reference: https://developers.google.com/analytics/devguides/collection/android/v4/dispatch