รหัสผู้ใช้ - Android SDK

คู่มือนักพัฒนาซอฟต์แวร์นี้สาธิตวิธีใช้ User ID โดยใช้ Google Analytics SDK สำหรับ Android v3.x

ภาพรวม

ฟีเจอร์ User-ID ช่วยวัดกิจกรรมผู้ใช้ที่ครอบคลุมหลายอุปกรณ์ใน Google Analytics เช่น การระบุแหล่งที่มาของการโต้ตอบกับแคมเปญการตลาดในอุปกรณ์เคลื่อนที่เครื่องหนึ่งต่อ Conversion ที่เกิดขึ้นในอุปกรณ์เคลื่อนที่อีกเครื่องหนึ่งหรือในเบราว์เซอร์

เมื่อมีการส่ง User ID พร้อม Hit ของ Google Analytics โดยใช้ช่อง userId รายงานของคุณจะแสดงจำนวนผู้ใช้ที่ไม่ซ้ำที่ถูกต้องแม่นยำยิ่งขึ้น และนำเสนอตัวเลือกการรายงานจากหลายอุปกรณ์ใหม่ๆ ดูข้อมูลเพิ่มเติมเกี่ยวกับประโยชน์ของการใช้ User ID

คู่มือนี้จะแสดงวิธีใช้ช่อง userId และ Google Analytics SDK สําหรับ Android ในการส่ง User-ID ไปยัง Google Analytics

ข้อกำหนดเบื้องต้น

ก่อนที่จะส่ง User ID ไปยัง Google Analytics:

การใช้งาน

เมื่อทราบว่าแอปพลิเคชัน Android ของคุณเป็นผู้ใช้ คุณควรส่งรหัสที่แสดงถึงผู้ใช้ดังกล่าวพร้อม Hit ของ Google Analytics เช่น การดูหน้าเว็บ เหตุการณ์ ธุรกรรมอีคอมเมิร์ซ ฯลฯ โดยใช้ช่อง userId

หากต้องการส่ง User-ID ให้ตั้งค่าช่อง userId โดยใช้ ไวยากรณ์แอมเพอร์แซนด์ของ Measurement Protocol และชื่อพารามิเตอร์ Fields.USER_ID ตามที่แสดงในตัวอย่างนี้

/**
 * An example method called when a user signs in to an authentication system.
 *
 * @param User user represents a generic User object returned by an authentication system on sign in.
 */
public void onUserSignIn(User user) {

  Tracker t = GoogleAnalytics.getInstance(context).getTracker("UA-XXXX-Y");

  // You only need to set User ID on a tracker once. By setting it on the tracker, the ID will be
  // sent with all subsequent hits.
  t.set(Fields.USER_ID, user.getId());

  // This hit will be sent with the User ID value and be visible in User-ID-enabled views (profiles).
  t.send(MapBuilder
      .createEvent("UX",       // Event category (required)
                   "Sign In",  // Event action (required)
                   null,       // Event label
                   null)       // Event value
      .build()
  );
}