Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android analytics v4 most simplified

I'm trying to connect google Analytics to my apps in the simplest way possible. I want to implement analytics v4, because google said that they'll force to upgrade to it soon, so I don't want to do twice the same work.

Google's tutorial, in this case, is not very efficient. Merging what they said there and what I've found on the internet, I've made this steps:

In Android Manifest, I've add this permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

and this, under the Application tag:

<meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
    android:resource="@xml/analytics" />
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

I'm not sure of the necessity of the second meta...

I've wrote an xml file, named "analytics.xml" (/res/xml/) containing:

<!-- the Local LogLevel for Analytics -->
<string name="ga_logLevel">verbose</string>

<!-- Treat events as test events and don't send to google -->
<bool name="ga_dryRun">false</bool>

<!-- <integer name="ga_sessionTimeout">300</integer> -->

<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>

<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtExceptions">true</bool>

<string name="ga_trackingId">UA-52900555-1</string>

<!-- The screen names that will appear in reports -->
<screenName name="com.maik.adbconnect.EmpityActivity">
    Adb Activation
</screenName>
<screenName name="com.maik.adbconnect.wid_class">
    Widget call
</screenName>

Now, in EmpityActivity.java, I've wrote this, at beginning of onCreate method:

GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = analytics.newTracker(R.xml.analytics);
t.send(new HitBuilders.AppViewBuilder().build());

In LogCat, analytics says that is started and hits... but in my analytics review, under "real time users", says zero....

Additional questions:

  • screennames in xml file are needed?

  • ga_logLevel and ga_dryRun are deprecated? Because logcat warn me that those resources are not recognized

like image 538
Maik93 Avatar asked Jul 17 '14 13:07

Maik93


People also ask

What is Google Analytics v4?

Google Analytics 4 is an analytics service that enables you to measure traffic and engagement across your websites and apps. This documentation provides implementation instructions and reference materials geared towards a developer audience.

What is changing with Google Analytics 4?

What's changing. The simple version: Universal Analytics is getting replaced by Google Analytics 4 starting July 1, 2023. The official announcement: Google announced that “On July 1, 2023, standard Universal Analytics properties will no longer process data.

Is Google Analytics 4 ready to be used?

It's official: You must switch to Google Analytics 4 by July 2023. Google has officially announced that “Universal Analytics” (Google Analytics as your currently know it) will officially be sunsetting in July 2023. This means all websites should be preparing for the switch over to Google Analytics 4.

Is GA4 replacing Universal Analytics?

Yes, GA4 is replacing Universal Analytics as the default for GA's digital analytics measurement. According to Google, they'll begin sunsetting (aka phasing out) Universal Analytics on July 1, 2023. This doesn't mean your UA properties will stop working on that date.


1 Answers

The simplest way to setup Google Analytic v4 is to use Java code and configure single tracker from Application.onCreate(). This is also the fastest and the safest way to configure Analytics. You code would look like this:

public class MyApp extends Application {
    private final String TRACKER_ID = "UA-54994796-4";

    private static GoogleAnalytics analytics;
    private static Tracker tracker;
    public static GoogleAnalytics analytics() {return analytics;}
    public static Tracker tracker() {return tracker;}

    @Override
    public void onCreate() {
        super.onCreate();
        analytics = GoogleAnalytics.getInstance(this);
        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
        tracker = analytics.newTracker("UA-00000000-1");
        tracker.enableExceptionReporting(true);
        tracker.enableAutoActivityTracking(true);
    }
}

To send an event from anywhere in your app you can use:

MyApp.tracker().send(new HitBuilders.EventBuilder(
    "some category", " some action").build());

You need few changes to your ApplicationManifest.xml as well:

<manifest>
...
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  ...
  <application
        android:name=".MyApp">
    ...
  </application>
  ...
</manifest>

I would stay away from using any XML configuration files as its easy to get the XML configuration wrong. Configuring from Java have the benefit of the compiler validating your calls. Using code to configure your Analytics is also faster as it avoids the XML parsing overhead.

Screen names are optional. You can stick with using class names. Your report will show the class names instead of more human friendly names but its simpler to avoid the extra mapping.

dryRun and logLevel are not depreciated as of Google Play Services 7.0. You are using either incorrect name, provided incorrect value of placed them in the wrong section of the XML. This is the problem with XML configuration. There is only limited runtime validation and its easy to get things wrong and not notice the problem.

like image 92
djabi Avatar answered Sep 22 '22 07:09

djabi