最新Google Analytics在Android Studio工具中的集成使用

這裏寫圖片描述

前言

自動google發佈Android Studio以來,很多項目都轉到這了這個開發工具,Android Studio採用gradle進行構建,雖然某種意義上十分的自動化,但是對於用慣了Eclipse+ADT開發的人來講,最開始還是有很多的不適應,在第一次構建項目的時候,最好能讓你的網絡連接google,否則會有很多問題,因爲自動構建會從google服務上獲取相應的服務包,這是需要能夠訪問google網絡,最近項目更新,要集成Google Analytics, 遇到了一些小問題,這是始料未及的,這裏和大家分享一下!

Google Services導入

這裏我們爲什麼要導入Google Services呢?前面說過Gradle是自動話構建,所以很多時候都會用到google提供的插件進行,例如:

apply plugin: 'com.google.gms.google-services'

爲此,我們需要在你的項目build.gradle中引入這個插件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

然後再項目模塊文件中應用這個插件:

apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
}

apply plugin: 'com.google.gms.google-services'

上面代碼中,記住一定要在最後apply 這個plugin,否則項目會報錯誤。

創建Google Analytics分析賬戶

https://www.google.com/analytics/

通過上面的url,我們可以按照導航創建我們自己對應的平臺賬戶,這裏要創建android平臺,Google 分析註冊的賬戶名字一定要記好(這裏舉個例子叫MyDemo),之後我們在獲取配置文件時候會用到。

這裏寫圖片描述
這裏寫圖片描述

獲取配置文件

通過如下URL獲取google-services.json文件,之後我們需要應用這個文件在項目中

https://developers.google.com/analytics/devguides/collection/android/v4/start?configured=true

下圖中的app name我們要用到創建時候的名字(MyDemo)。

這裏寫圖片描述

配置Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.analytics">

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

  <application android:name="AnalyticsApplication">
    ...
  </application>
</manifest>

導入Google-Services.json

這裏寫圖片描述

在src同級目錄導入這個文件。

這裏寫圖片描述

在main的同級目錄創建兩個文件夾,debug和release,同樣導入這個文件,之所以這樣,是因爲google-service plugin 會用到這個路徑,否則會報錯。

實例化GoogleAnalytics

/*
 * Copyright Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.samples.quickstart.analytics;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

/**
 * This is a subclass of {@link Application} used to provide shared objects for this app, such as
 * the {@link Tracker}.
 */
public class AnalyticsApplication extends Application {
  private Tracker mTracker;

  /**
   * Gets the default {@link Tracker} for this {@link Application}.
   * @return tracker
   */
  synchronized public Tracker getDefaultTracker() {
    if (mTracker == null) {
      GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
      // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
      mTracker = analytics.newTracker(R.xml.global_tracker);
    }
    return mTracker;
  }
}

觸發Event事件

正常來講,我們需要統計某些頁面用戶訪問信息,所以需要集成這個事件,代碼示例:

mTracker.send(new HitBuilders.EventBuilder()
    .setCategory("Action")
    .setAction("Share")
    .build());

好了,可以build一下試試吧!

發佈了208 篇原創文章 · 獲贊 143 · 訪問量 79萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章