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_重新開啟動態連結導致應用程式重新開啟的次數