API การวิเคราะห์ลิงก์แบบไดนามิกของ Firebase

คุณสามารถใช้ REST API นี้เพื่อรับข้อมูลการวิเคราะห์สำหรับลิงก์ไดนามิกแบบสั้นแต่ละรายการของคุณ ไม่ว่า จะสร้างขึ้นในคอนโซลหรือโดยทางโปรแกรมก็ตาม

การอนุญาต API

เมื่อคุณส่งคำขอไปยัง Dynamic Link Analytics API คุณต้องรวมโทเค็นการเข้าถึง OAuth 2.0 ที่ให้สิทธิ์การเข้าถึงโปรเจ็กต์ Firebase ของคุณ

คุณสามารถรับโทเค็นการเข้าถึงได้โดยใช้ไลบรารีไคลเอนต์ Google API:

  1. เพิ่ม Firebase ลงในแอปของคุณ ตามที่อธิบายไว้ในคู่มือการตั้งค่า Admin SDK นั่นคือสร้างบัญชีบริการและสร้างรหัสส่วนตัว
  2. ใช้ไลบรารีไคลเอ็นต์ Google API เพื่อรับโทเค็นการเข้าถึงจากข้อมูลรับรองบัญชีบริการของคุณ:

    ชวา

    การใช้ ไลบรารีไคลเอ็นต์ Google API สำหรับ 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.
    

    โหนด js

    การใช้ ไลบรารีไคลเอ็นต์ Google API สำหรับ 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.
      }
    });
    

    หลาม

    การใช้ไลบรารี Google Auth สำหรับ 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
    

รับสถิติสำหรับลิงก์ไดนามิกลิงก์เดียว

ใช้ตำแหน่งข้อมูล linkStats เพื่อรับสถิติเหตุการณ์สำหรับไดนามิกลิงก์เดียว

คำขอ HTTP

คำขอ linkStats มีรูปแบบดังต่อไปนี้:

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

Authorization: Bearer ACCESS_TOKEN

ตัวอย่างเช่น หากต้องการดึงสถิติในช่วง 7 วันที่ผ่านมาสำหรับลิงก์แบบสั้น 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...
พารามิเตอร์
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 เพื่อติดตั้งหรืออัปเดตแอป หรือไปยังปลายทางอื่น
APP_ติดตั้ง จำนวนการติดตั้งจริง (รองรับโดย Play Store เท่านั้น)
APP_FIRST_เปิด จำนวนการเปิดครั้งแรกหลังการติดตั้ง
APP_RE_เปิด จำนวนครั้งที่ไดนามิกลิงก์ทำให้แอปเปิดอีกครั้ง