广告系列衡量

本文档将简要介绍如何使用 Android 版 Google Analytics(分析)SDK v4 来衡量广告系列和流量来源。

概览

通过在 Google Analytics(分析)中衡量广告系列,可将您应用中的用户活动归因于特定的广告系列和流量来源。以下选项可用于在 Android 版 Google Analytics(分析)SDK v4 中进行广告系列和流量来源归因。

下文将介绍何时及如何在您的应用中实现每种广告系列衡量功能。

Google Play 广告系列归因

利用 Google Play 广告系列衡量功能,您可以了解是哪些广告系列和流量来源将用户引荐到 Google Play 商店下载您的应用。我们建议所有开发者都实现 Google Play 商店广告系列衡量功能。

实现 Google Play 广告系列归因

Google Play 商店提供了 Install Referrer API,以便开发者从 Google Play 安全地检索引荐来源内容。此 API 会返回用于访问您的应用的 Google Play 商店页面的引荐来源网址参数的值(如果存在)。

要将某次应用下载归因到某个广告系列,您必须在指向 Google Play 商店的所有链接中添加引荐来源网址参数,并向您的应用添加 Play 的 Install Referrer API 才能在您的 Google Analytics(分析)跟踪器中接收并设置 intent 中包含的广告系列信息。

1. 从 AndroidManifest.xml 文件中移除 Google Analytics(分析)接收器

如果您还实现了 Google 跟踪代码管理器接收器,请同时从清单中移除该接收器。

2. 添加对 Install Referrer API 库的依赖项。

要添加依赖项,请在 build.gradle 中添加以下代码:

dependencies {
    ...
    implementation 'com.android.installreferrer:installreferrer:1.1'
}

3. 在应用的启动 activity 中调用 Install Referrer API。

使用 Install Referrer API 获取已安装应用软件包的引荐来源网址,然后将网址值传递给 Google Analytics(分析)或 Google 跟踪代码管理器接收器。按照与以下代码类似的实现方式,在您应用的启动 Activity 中使用 Install Referrer API。

如果应用有多个入口点(例如特定部分的深层链接),您可以在由 ActivityLifecycleCallbacks 触发的 ActivityLifecycleListener 中实现这些既定方法。

package com.example.myapplication;

import static com.android.installreferrer.api.InstallReferrerClient.InstallReferrerResponse;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
import com.google.android.gms.analytics.CampaignTrackingReceiver;
import com.google.tagmanager.InstallReferrerReceiver;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    private final Executor backgroundExecutor = Executors.newSingleThreadExecutor();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkInstallReferrer();
    }

    // TODO: Change this to use whatever preferences are appropriate. The install referrer should
    // only be sent to the receiver once.
    private final String prefKey = "checkedInstallReferrer";

    void checkInstallReferrer() {
        if (getPreferences(MODE_PRIVATE).getBoolean(prefKey, false)) {
            return;
        }

        InstallReferrerClient referrerClient = InstallReferrerClient.newBuilder(this).build();
        backgroundExecutor.execute(() -> getInstallReferrerFromClient(referrerClient));
    }

    void getInstallReferrerFromClient(InstallReferrerClient referrerClient) {

        referrerClient.startConnection(new InstallReferrerStateListener() {
            @Override
            public void onInstallReferrerSetupFinished(int responseCode) {
                switch (responseCode) {
                    case InstallReferrerResponse.OK:
                        ReferrerDetails response = null;
                        try {
                            response = referrerClient.getInstallReferrer();
                        } catch (RemoteException e) {
                            e.printStackTrace();
                            return;
                        }
                        final String referrerUrl = response.getInstallReferrer();


                        // TODO: If you're using GTM, call trackInstallReferrerforGTM instead.
                        trackInstallReferrer(referrerUrl);


                        // Only check this once.
                        getPreferences(MODE_PRIVATE).edit().putBoolean(prefKey, true).commit();

                        // End the connection
                        referrerClient.endConnection();

                        break;
                    case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                        // API not available on the current Play Store app.
                        break;
                    case InstallReferrerResponse.SERVICE_UNAVAILABLE:
                        // Connection couldn't be established.
                        break;
                }
            }

            @Override
            public void onInstallReferrerServiceDisconnected() {

            }
        });
    }

    // Tracker for Classic GA (call this if you are using Classic GA only)
    private void trackInstallReferrer(final String referrerUrl) {
        new Handler(getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                CampaignTrackingReceiver receiver = new CampaignTrackingReceiver();
                Intent intent = new Intent("com.android.vending.INSTALL_REFERRER");
                intent.putExtra("referrer", referrerUrl);
                receiver.onReceive(getApplicationContext(), intent);
            }
        });
    }

    // Tracker for GTM + Classic GA (call this if you are using GTM + Classic GA only)
    private void trackInstallReferrerforGTM(final String referrerUrl) {
        new Handler(getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                InstallReferrerReceiver receiver = new InstallReferrerReceiver();
                Intent intent = new Intent("com.android.vending.INSTALL_REFERRER");
                intent.putExtra("referrer", referrerUrl);
                receiver.onReceive(getApplicationContext(), intent);
            }
        });
    }

}

4. 将 Google Analytics(分析)广告系列参数添加到 Google Play 网址

接下来,向任何直接链接到 Google Play 商店的网址添加 referrer 参数,并将该参数的值设置为用于描述来源的 Google Analytics(分析)广告系列参数字符串,如下例所示:

https://play.google.com/store/apps/details?id=com.example.application
&referrer=utm_source%3Dgoogle
%26utm_medium%3Dcpc
%26utm_term%3Drunning%252Bshoes
%26utm_content%3Dlogolink
%26utm_campaign%3Dspring_sale

要了解如何构建广告系列参数字符串,请使用 Google Play 网址构建工具,或参阅广告系列参数一节。

常规广告系列和流量来源归因

应用在安装之后,即可被来自广告系列、网站或其他应用的引荐所启动。在这种情况下,您可以使用 setCampaignParamsFromUrl 方法直接在跟踪器上设置广告系列参数,以便将后续会话中的用户活动归因到特定的引荐流量来源或营销广告系列。

// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
    TrackerName.APP_TRACKER);

// Set screen name.
t.setScreenName(screenName);

// In this example, campaign information is set using
// a url string with Google Analytics campaign parameters.
// Note: This is for illustrative purposes. In most cases campaign
//       information would come from an incoming Intent.
String campaignData = "http://examplepetstore.com/index.html?" +
    "utm_source=email&utm_medium=email_marketing&utm_campaign=summer" +
    "&utm_content=email_variation_1";

// Campaign data sent with this hit.
t.send(new HitBuilders.ScreenViewBuilder()
    .setCampaignParamsFromUrl(campaignData)
    .build()
);

如需详细了解 getTracker 方法,请参阅 高级配置

广告系列参数

广告系列参数用于传递将用户带到您的应用中的流量来源和广告系列的相关信息。

下表列出了可用的广告系列参数,这些参数可用于 Google Play 广告系列衡量或常规广告系列衡量:

参数 说明 示例
utm_source 广告系列来源,用于确定具体的搜索引擎、简报或其他来源 utm_source=google
utm_medium 广告系列媒介,用于确定电子邮件或采用每次点击费用 (CPC) 的广告等媒介。 utm_medium=cpc
utm_term 广告系列字词,用于付费搜索,为广告提供关键字 utm_term=running+shoes
utm_content 广告系列内容,用于 A/B 测试和内容定位广告,以区分指向相同网址的不同广告或链接 utm_content=logolink
utm_content=textlink
utm_campaign 广告系列名称,用于关键字分析,以标识具体的产品推广活动或战略广告系列 utm_campaign=spring_sale
gclid Google Ads 自动标记参数,用于衡量广告。此值会动态生成,请勿修改。

Google Play 网址构建工具

请使用下面的工具为 Google Play 广告系列衡量功能生成网址。