Firebase 动态链接分析 API

您可以使用此 REST API 获取每个短动态链接的分析数据,无论是在控制台中创建还是以编程方式创建

API授权

当您向动态链接分析 API 发出请求时,您必须包含一个 OAuth 2.0 访问令牌,用于授权访问您的 Firebase 项目。

您可以使用 Google API 客户端库获取访问令牌:

  1. 按照 Admin SDK 设置指南中的说明将 Firebase 添加到您的应用。即创建服务帐号并生成私钥。
  2. 使用 Google API 客户端库从您的服务帐户凭据获取访问令牌:

    爪哇

    使用适用于 Java 的 Google API 客户端库

    // Load the service account key JSON file
    FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
    
    // Authenticate a Google credential with the service account
    GoogleCredential googleCred = GoogleCredential.fromStream(serviceAccount);
    
    // Add the required scope to the Google credential
    GoogleCredential scoped = googleCred.createScoped(
        Arrays.asList(
          "https://www.googleapis.com/auth/firebase"
        )
    );
    
    // Use the Google credential to generate an access token
    scoped.refreshToken();
    String token = scoped.getAccessToken();
    
    // Include the access token in the Authorization header.
    

    Node.js

    使用Node.js 的 Google API 客户端库

    var { google } = require("googleapis");
    
    // Load the service account key JSON file.
    var serviceAccount = require("path/to/serviceAccountKey.json");
    
    // Specify the required scope.
    var scopes = [
      "https://www.googleapis.com/auth/firebase"
    ];
    
    // Authenticate a JWT client with the service account.
    var jwtClient = new google.auth.JWT(
      serviceAccount.client_email,
      null,
      serviceAccount.private_key,
      scopes
    );
    
    // Use the JWT client to generate an access token.
    jwtClient.authorize(function(error, tokens) {
      if (error) {
        console.log("Error making request to generate access token:", error);
      } else if (tokens.access_token === null) {
        console.log("Provided service account does not have permission to generate access tokens");
      } else {
        var accessToken = tokens.access_token;
    
        // Include the access token in the Authorization header.
      }
    });
    

    Python

    使用适用于 Python 的Google Auth库:

    from google.oauth2 import service_account
    from google.auth.transport.requests import AuthorizedSession
    
    # Specify the required scope
    scopes = [
      "https://www.googleapis.com/auth/firebase"
    ]
    
    # Authenticate a credential with the service account
    credentials = service_account.Credentials.from_service_account_file(
        "path/to/serviceAccountKey.json", scopes=scopes)
    
    # Use the credentials object to authenticate a Requests session.
    authed_session = AuthorizedSession(credentials)
    response = authed_session.get(
        "https://firebasedynamiclinks.googleapis.com/v1/SHORT_DYNAMIC_LINK/linkStats?durationDays=DURATION")
    
    # Or, use the token directly, as described below.
    request = google.auth.transport.requests.Request()
    credentials.refresh(request)
    access_token = credentials.token
    

获取单个动态链接的统计信息

使用linkStats端点获取单个动态链接的事件统计信息。

HTTP请求

linkStats请求具有以下格式:

GET https://firebasedynamiclinks.googleapis.com/v1/SHORT_DYNAMIC_LINK/linkStats?durationDays=DURATION

Authorization: Bearer ACCESS_TOKEN

例如,要检索短链接https://example.page.link/wXYz过去 7 天的统计信息:

GET https://firebasedynamiclinks.googleapis.com/v1/https%3A%2F%2Fexample.page.link%2FwXYz/linkStats?durationDays=7

Authorization: Bearer ya29.Abc123...
参数
SHORT_DYNAMIC_LINK您想要获取其事件数据的URL 编码的短动态链接。
DURATION获取事件数据的天数。例如,如果您指定30 ,则请求将检索过去 30 天的数据。请注意,过去 36 小时内记录的某些事件可能不包括在内。
ACCESS_TOKEN未过期的访问令牌。请参阅API 授权

响应体

对请求的响应是一个 JSON 对象,如下所示:

{
  "linkEventStats": [
    {
      "platform": "ANDROID",
      "count": "123",
      "event": "CLICK"
    },
    {
      "platform": "IOS",
      "count": "123",
      "event": "CLICK"
    },
    {
      "platform": "DESKTOP",
      "count": "456",
      "event": "CLICK"
    },
    {
      "platform": "ANDROID",
      "count": "99",
      "event": "APP_INSTALL"
    },
    {
      "platform": "ANDROID",
      "count": "42",
      "event": "APP_FIRST_OPEN"
    },

    ...

  ]
}

linkEventStats列表中的每一项都包含一些动态链接相关事件的特定于平台的计数(例如 Android 上的点击次数)。请注意,这些统计信息可能不包括过去 36 小时内记录的事件。

事件描述Firebase 控制台休息API
点击动态链接上的任何点击计数,无论其处理方式及其目的地如何
重定向尝试将用户重定向到 App Store 或 Play Store 以安装或更新应用程序,或者重定向到某个其他目的地的次数
应用程序_安装实际安装次数(仅 Play 商店支持)
APP_FIRST_OPEN安装后首次打开的次数
APP_重新打开动态链接导致应用程序重新打开的次数
,

您可以使用此 REST API 获取每个短动态链接的分析数据,无论是在控制台中创建还是以编程方式创建

API授权

当您向动态链接分析 API 发出请求时,您必须包含一个 OAuth 2.0 访问令牌,用于授权访问您的 Firebase 项目。

您可以使用 Google API 客户端库获取访问令牌:

  1. 按照 Admin SDK 设置指南中的说明将 Firebase 添加到您的应用。即创建服务帐号并生成私钥。
  2. 使用 Google API 客户端库从您的服务帐户凭据获取访问令牌:

    爪哇

    使用适用于 Java 的 Google API 客户端库

    // Load the service account key JSON file
    FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
    
    // Authenticate a Google credential with the service account
    GoogleCredential googleCred = GoogleCredential.fromStream(serviceAccount);
    
    // Add the required scope to the Google credential
    GoogleCredential scoped = googleCred.createScoped(
        Arrays.asList(
          "https://www.googleapis.com/auth/firebase"
        )
    );
    
    // Use the Google credential to generate an access token
    scoped.refreshToken();
    String token = scoped.getAccessToken();
    
    // Include the access token in the Authorization header.
    

    Node.js

    使用Node.js 的 Google API 客户端库

    var { google } = require("googleapis");
    
    // Load the service account key JSON file.
    var serviceAccount = require("path/to/serviceAccountKey.json");
    
    // Specify the required scope.
    var scopes = [
      "https://www.googleapis.com/auth/firebase"
    ];
    
    // Authenticate a JWT client with the service account.
    var jwtClient = new google.auth.JWT(
      serviceAccount.client_email,
      null,
      serviceAccount.private_key,
      scopes
    );
    
    // Use the JWT client to generate an access token.
    jwtClient.authorize(function(error, tokens) {
      if (error) {
        console.log("Error making request to generate access token:", error);
      } else if (tokens.access_token === null) {
        console.log("Provided service account does not have permission to generate access tokens");
      } else {
        var accessToken = tokens.access_token;
    
        // Include the access token in the Authorization header.
      }
    });
    

    Python

    使用适用于 Python 的Google Auth库:

    from google.oauth2 import service_account
    from google.auth.transport.requests import AuthorizedSession
    
    # Specify the required scope
    scopes = [
      "https://www.googleapis.com/auth/firebase"
    ]
    
    # Authenticate a credential with the service account
    credentials = service_account.Credentials.from_service_account_file(
        "path/to/serviceAccountKey.json", scopes=scopes)
    
    # Use the credentials object to authenticate a Requests session.
    authed_session = AuthorizedSession(credentials)
    response = authed_session.get(
        "https://firebasedynamiclinks.googleapis.com/v1/SHORT_DYNAMIC_LINK/linkStats?durationDays=DURATION")
    
    # Or, use the token directly, as described below.
    request = google.auth.transport.requests.Request()
    credentials.refresh(request)
    access_token = credentials.token
    

获取单个动态链接的统计信息

使用linkStats端点获取单个动态链接的事件统计信息。

HTTP请求

linkStats请求具有以下格式:

GET https://firebasedynamiclinks.googleapis.com/v1/SHORT_DYNAMIC_LINK/linkStats?durationDays=DURATION

Authorization: Bearer ACCESS_TOKEN

例如,要检索短链接https://example.page.link/wXYz过去 7 天的统计信息:

GET https://firebasedynamiclinks.googleapis.com/v1/https%3A%2F%2Fexample.page.link%2FwXYz/linkStats?durationDays=7

Authorization: Bearer ya29.Abc123...
参数
SHORT_DYNAMIC_LINK您想要获取其事件数据的URL 编码的短动态链接。
DURATION获取事件数据的天数。例如,如果您指定30 ,则请求将检索过去 30 天的数据。请注意,过去 36 小时内记录的某些事件可能不包括在内。
ACCESS_TOKEN未过期的访问令牌。请参阅API 授权

响应体

对请求的响应是一个 JSON 对象,如下所示:

{
  "linkEventStats": [
    {
      "platform": "ANDROID",
      "count": "123",
      "event": "CLICK"
    },
    {
      "platform": "IOS",
      "count": "123",
      "event": "CLICK"
    },
    {
      "platform": "DESKTOP",
      "count": "456",
      "event": "CLICK"
    },
    {
      "platform": "ANDROID",
      "count": "99",
      "event": "APP_INSTALL"
    },
    {
      "platform": "ANDROID",
      "count": "42",
      "event": "APP_FIRST_OPEN"
    },

    ...

  ]
}

linkEventStats列表中的每一项都包含一些动态链接相关事件的特定于平台的计数(例如 Android 上的点击次数)。请注意,这些统计信息可能不包括过去 36 小时内记录的事件。

事件描述Firebase 控制台休息API
点击动态链接上的任何点击计数,无论其处理方式及其目的地如何
重定向尝试将用户重定向到 App Store 或 Play Store 以安装或更新应用程序,或者重定向到某个其他目的地的次数
应用程序_安装实际安装次数(仅 Play 商店支持)
APP_FIRST_OPEN安装后首次打开的次数
APP_重新打开动态链接导致应用程序重新打开的次数