android studio google analytics example
in this article, today learn android studio google analytics example.
1. first add to build dependency :
implementation ('com.google.android.gms:play-services:10.2.4') {
exclude group: "com.android.support", module: "support-v4"
}
2. MainActivity.java:
package com.Analytics;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
/**
* Demo of one activity that has tracking enabled.
*/
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Track the page view for the Activity
Tracker tracker =
((GADemoApp)getApplication()).getTracker();
tracker.setScreenName("MainActivity");
tracker.send(new HitBuilders.ScreenViewBuilder().build());
/* You can track events like button clicks... */
findViewById(R.id.actionButton).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Tracker tracker =
((GADemoApp)getApplication()).getTracker();
tracker.send(new HitBuilders.EventBuilder(
"Action Event", "Button Clicked").build());
}
});
// Rest of the Activity initialization here...
}
}
3. create a class GADemoApp.java :
package com.Analytics;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
public class GADemoApp extends Application {
/*
* Define web property ID obtained after creating a profile for the app. If
* using the Gradle plugin, this should be available as R.xml.global_tracker.
*/
private String webId = "UA-NNNNNNNN-Y";
/* Analytics tracker instance */
Tracker tracker;
/* This is the getter for the tracker instance. This is called from
* within the Activity to get a reference to the tracker instance.
*/
public synchronized Tracker getTracker() {
if (tracker == null) {
// Get the singleton Analytics instance, get Tracker from it
GoogleAnalytics instance = GoogleAnalytics.getInstance(this);
// Start tracking the app with your web property ID
tracker = instance.newTracker(webId);
// Any app-specific Application setup code goes here...
}
return tracker;
}
}
4.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/actionButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
5.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Analytics">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
6.strings.xml:
<resources>
<string name="app_name">Analytics</string>
<string name="hello">hello</string>
</resources>
No comments:
Post a Comment