API Analisis Firebase Dynamic Links

Anda dapat menggunakan REST API ini untuk mendapatkan data analisis dari setiap Dynamic Links singkat, baik dibuat di konsol maupun secara terprogram.

Otorisasi API

Saat membuat permintaan ke Dynamic Link Analytics API, Anda harus menyertakan OAuth Token akses 2.0 yang mengizinkan akses ke project Firebase Anda.

Anda bisa mendapatkan token akses menggunakan library klien Google API:

  1. Tambahkan Firebase ke aplikasi sebagai yang dijelaskan dalam panduan penyiapan Admin SDK. Artinya, buat akun layanan dan membuat kunci pribadi.
  2. Menggunakan library klien Google API untuk mendapatkan token akses dari layanan Anda kredensial akun:

    Java

    Menggunakan Library Klien Google API untuk Java:

    // 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

    Menggunakan Library Klien Google API untuk Node.js:

    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

    Menggunakan Library Google Auth untuk Python:

    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
    

Dapatkan statistik untuk satu Dynamic Link

Gunakan endpoint linkStats guna mendapatkan statistik peristiwa untuk satu Dynamic Link.

Permintaan HTTP

Permintaan linkStats memiliki format berikut:

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

Authorization: Bearer ACCESS_TOKEN

Misalnya, untuk mengambil statistik dari 7 hari terakhir untuk link pendek https://example.page.link/wXYz:

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

Authorization: Bearer ya29.Abc123...
Parameter
SHORT_DYNAMIC_LINK Dynamic Link singkat yang dienkode URL yang data peristiwanya ingin Anda dapatkan.
DURATION Jumlah hari untuk mendapatkan data peristiwa. Misalnya, jika Anda menentukan 30, permintaan akan mengambil data selama 30 hari terakhir. Perhatikan bahwa beberapa peristiwa yang dicatat dalam 36 jam terakhir mungkin tidak disertakan.
ACCESS_TOKEN Token akses yang masih berlaku. Lihat API Otorisasi.

Isi respons

Respons terhadap permintaan adalah objek JSON seperti berikut:

{
  "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"
    },

    ...

  ]
}

Setiap item dalam daftar linkEventStats berisi jumlah khusus platform beberapa peristiwa terkait Dynamic Link (seperti jumlah klik pada Android). Perlu diketahui bahwa statistik ini mungkin tidak menyertakan peristiwa yang telah dicatat dalam 36 jam.

Peristiwa Deskripsi Konsol Firebase REST API
CLICK Jumlah klik pada Dynamic Link, terlepas dari cara penanganannya dan tujuannya
REDIRECT Jumlah upaya untuk mengalihkan pengguna, baik ke App Store atau Play Store untuk menginstal atau mengupdate aplikasi, atau ke tujuan lainnya
APP_INSTALL Jumlah penginstalan yang sebenarnya (hanya didukung oleh Play Store)
APP_FIRST_OPEN Jumlah pertama kali dibuka setelah diinstal
APP_RE_OPEN Berapa kali Dynamic Link menyebabkan aplikasi dibuka kembali