{"id":58,"date":"2017-04-26T05:14:26","date_gmt":"2017-04-26T05:14:26","guid":{"rendered":"http:\/\/www.analyticsbook.org\/?page_id=58"},"modified":"2021-02-19T01:27:21","modified_gmt":"2021-02-19T01:27:21","slug":"google-mobile-app-analytics-android-sdk","status":"publish","type":"page","link":"https:\/\/www.analyticsbook.org\/google-mobile-app-analytics-android-sdk\/","title":{"rendered":"Google Mobile App Analytics Android SDK"},"content":{"rendered":"
\t\t\t\t\t\t\t
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tGordon Choi
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAuthor<\/p>\n
<\/p>\n
Let’s implement the Google Analytics SDK onto your Android app. This Android SDK implementation assumes you use Android Studio and Google Play Services.<\/p>\n
In your Android app, add INTERNET and ACCESS_NETWORK_STATE permissions to the AndroidManifest.xml file.<\/p>\n
\n ...\n<\/pre>\nBuild.gradle (Project-level & App-level)<\/h2>\n
In your Android app, it should have a build.gradle file on the project level, and a second build.gradle file on the app level.<\/p>\n
Add the dependency to your project-level build.gradle:<\/p>\n
classpath 'com.google.gms:google-services:3.0.0'<\/pre>\nAdd the plugin to your app-level build.gradle:<\/p>\n
apply plugin: 'com.google.gms.google-services'<\/pre>\nAdd the dependency to your app-level build.gradle:<\/p>\n
compile 'com.google.android.gms:play-services-analytics:9.2.0'<\/pre>\nConfiguration File<\/h2>\n
Download the google-services.json file (i.e. configuration file). Copy and paste the configuration file onto the app\/ directory of your Android Studio project.<\/p>\n
https:\/\/developers.google.com\/mobile\/add?platform=android&cntapi=analytics&cnturl=https:%2F%2Fdevelopers.google.com%2Fanalytics%2Fdevguides%2Fcollection%2Fandroid%2Fv4%2Fapp%3Fconfigured%3Dtrue&cntlbl=Continue%20Adding%20Analytics<\/pre>\nAnalyticsApplication.java<\/h2>\n
Create a new file AnalyticsApplication.java which extends the Application and provides a helper method that returns your application’s tracker.<\/p>\n
package com.google.samples.quickstart.analytics;\nimport android.app.Application;\nimport com.google.android.gms.analytics.GoogleAnalytics;\nimport com.google.android.gms.analytics.Tracker;\npublic class AnalyticsApplication extends Application {\n private Tracker mTracker;\n synchronized public Tracker getDefaultTracker() {\n if (mTracker == null) {\n GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);\n mTracker = analytics.newTracker(R.xml.global_tracker);\n }\n return mTracker;\n }\n}<\/pre>\nMainActivity.java<\/h2>\n
Let’s add the tracking codes to Activities or Fragments of your Android app. The setup allows screen views to be tracked.<\/p>\n
In the onCreate method of your activity files (e.g. MainActivity, NextScreenActivity, etc) or the fragment files (e.g. FragmentActivity, etc), add the following:<\/p>\n
AnalyticsApplication application = (AnalyticsApplication) getApplication();\nmTracker = application.getDefaultTracker();<\/pre>\nIn the onResume method of your activity files or onPageSelected of ViewPager, add the following:<\/p>\n
Log.i(TAG, \"Setting screen name: \" + name);\nmTracker.setScreenName(\"Image~\" + name);\nmTracker.send(new HitBuilders.ScreenViewBuilder().build());<\/pre>\nAdd these same codes to all Activities or Fragments which represent \u201cscreens\u201d.<\/p>\n
\nPrevious Chapters<\/h4>\n
\n
- Chapter 16 – Google Mobile App Analytics iOS SDK<\/a><\/li>\n<\/ul>\n
Next Chapters<\/h4>\n
\n
- Chapter 18 – Definitions of Metrics & Dimensions<\/a><\/li>\n
- Chapter 19 – Web Server Log Analytics<\/a><\/li>\n
- Chapter 20 – Skills Web Analysts & Mobile App Analysts Must Have<\/a><\/li>\n<\/ul>\n
\n<\/ins><\/p>\n
\n<\/p>\n
Content on Gordon Choi’s Analytics Book is licensed under the CC Attribution-Noncommercial 4.0 International license.<\/em><\/p>\n
Gordon Choi’s Other Books:
The China Mobile SEO Book<\/a>
Mobile Website Book<\/a>
<\/em><\/p>\n<\/p>\n
<\/p>\n
[elementor-template id=”764″]\t\t<\/p>\n
Copyright 2016-2021 www.AnalyticsBook.org<\/p>\n","protected":false},"excerpt":{"rendered":"
How To Add Google Analytics In Android App? Gordon Choi Author Chapter 17 – Google Mobile App Analytics Android SDK AndroidManifest.xml Build.gradle (Project-level & App-level) Configuration File AnalyticsApplication.java MainActivity.java Let’s implement the Google Analytics SDK onto your Android app. This Android SDK implementation assumes you use Android Studio and Google Play Services. AndroidManifest.xml In your […]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"elementor_canvas","meta":{"footnotes":""},"yoast_head":"\n
Google Mobile App Analytics Android SDK - Gordon Choi's Analytics Book<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n