เอกสารนี้อธิบายภาพรวมเกี่ยวกับฟีเจอร์การกำหนดค่าขั้นสูงบางอย่างของ Google Analytics SDK สำหรับ Android เวอร์ชัน 2
ภาพรวม
Google Analytics SDK สำหรับ Android ใช้ 2 คลาสเพื่อจัดการสถานะส่วนกลางของการติดตั้งใช้งานและส่งข้อมูลไปยังเซิร์ฟเวอร์ Google Analytics EasyTracker รวมคลาสเหล่านี้เพื่อให้ การกำหนดค่าและการจัดการเซสชันทำได้ง่ายขึ้น
GoogleAnalytics
– ซิงเกิลตันที่จัดการสถานะส่วนกลางของการติดตั้งใช้งาน ซึ่งรวมถึงการรับออบเจ็กต์Tracker
Tracker
– ชั้นเรียนที่คุณใช้ส่งข้อมูลไปยัง Google Analytics
ตัวอย่างเช่น หากต้องการวัดมุมมองของ Activity
แบบง่าย ให้ทำดังนี้
import android.app.Activity import com.google.analytics.tracking.android.GoogleAnalytics import com.google.analytics.tracking.android.Tracker /** * A simple Activity that sends a screen view to Google Analytics * when it is displayed to the user. */ public class HomeScreen extends Activity { private Tracker mGaTracker; private GoogleAnalytics mGaInstance; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the GoogleAnalytics singleton. Note that the SDK uses // the application context to avoid leaking the current context. mGaInstance = GoogleAnalytics.getInstance(this); // Use the GoogleAnalytics singleton to get a Tracker. mGaTracker = mGaInstance.getTracker("UA-XXXX-Y"); // Placeholder tracking ID. ... // The rest of your onCreate() code. } @Override public void onStart() { super.onStart(); // Send a screen view when the Activity is displayed to the user. mGaTracker.sendView("/HomeScreen"); } }
การใช้ตัวติดตามหลายอย่าง
ตั้งแต่ SDK เวอร์ชัน 2 คุณสามารถใช้เครื่องมือติดตามหลายตัวในการติดตั้งใช้งานเพียงครั้งเดียว โดยสามารถใช้ 1 ตัวต่อรหัสติดตามที่ไม่ซ้ำกัน อุปกรณ์ติดตามทั้งหมดอยู่ในรัฐเดียวกัน
ที่ถือโดยซิงเกิล GoogleAnalytics
ในตัวอย่างต่อไปนี้ ระบบจะส่งการดูหน้าจอไปยังพร็อพเพอร์ตี้ 2 รายการแยกกันโดยใช้เครื่องมือติดตาม 2 รายการ โดยแต่ละตัวมีรหัสพร็อพเพอร์ตี้ที่ไม่ซ้ำกันเป็นของตนเอง
import android.app.Activity import com.google.analytics.tracking.android.GoogleAnalytics import com.google.analytics.tracking.android.Tracker /** * A simple Activity that sends a screen view to Google Analytics * when it is displayed to the user. */ public class HomeScreen extends Activity { private GoogleAnalytics mGaInstance; private Tracker mGaTracker1; private Tracker mGaTracker2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the GoogleAnalytics singleton. mGaInstance = GoogleAnalytics.getInstance(this); // Use the GoogleAnalytics singleton to get two Trackers with // unique property IDs. mGaTracker1 = mGaInstance.getTracker("UA-XXXX-Y"); mGaTracker2 = mGaInstance.getTracker("UA-XXXX-Z"); ... // The rest of your onCreate() code. } @Override public void onStart() { super.onStart(); // Send a screen view to "UA-XXXX-Y" the Activity is displayed to the user. mGaTracker1.sendView("/HomeScreen"); // Send another screen view to the second property, "UA-XXXX-Z". mGaTracker2.sendView("/Home"); } }
โปรดทราบว่าฟีเจอร์การวัดผลอัตโนมัติจาก EasyTracker จะใช้เครื่องมือติดตามเริ่มต้นในการโทรเท่านั้น หากคุณกำลังใช้คุณลักษณะ EasyTracker เหล่านี้และต้องการส่งข้อมูลโดยใช้ตัวติดตามอื่นๆ คุณจะต้องทำด้วยตนเอง
เครื่องมือติดตามเริ่มต้น
แม้ว่าการติดตั้งใช้งานอาจใช้อุปกรณ์ติดตามหลายอย่าง แต่ทั่วโลกจะมีอุปกรณ์ติดตามเริ่มต้นเพียงรายการเดียว Tracker
แรกที่ดึงมาจะกลายเป็นเครื่องมือติดตามเริ่มต้น
หากต้องการใช้ตัวติดตามเริ่มต้น ให้ใช้
// Get singleton. GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this); // Get default tracker. Tracker myDefault = myInstance.getDefaultTracker();
หากต้องการตั้งค่าตัวติดตามเริ่มต้น ให้ใช้
// First get a tracker using a new property ID. Tracker newTracker = myInstance.getTracker("UA-XXXX-2"); // Then make newTracker the default tracker globally. myInstance.setDefaultTracker(newTracker);
การจ่ายงาน
การตั้งค่าการส่งงานได้รับการจัดการโดย Singleton GAServiceManager
วิธีกำหนดระยะเวลาการจัดส่ง
// Set dispatch period to 30 seconds. GAServiceManager.getInstance().setDispatchPeriod(30);
วิธีจ่ายงานด้วยตนเองนอกระยะเวลาที่กำหนด
// Manually dispatch any queued hits. GAServiceManager.getInstance().dispatch();
ทดสอบผลิตภัณฑ์
คุณสามารถเปิดใช้การสุ่มตัวอย่างฝั่งไคลเอ็นต์เพื่อจำกัดจำนวน Hit ที่ส่งไปยัง Google Analytics หากแอปมีผู้ใช้จำนวนมากหรือส่งข้อมูลปริมาณมากไปยัง Google Analytics การเปิดใช้การสุ่มตัวอย่างจะช่วยให้การรายงานไม่ขาดตอน
ตัวอย่างเช่น หากต้องการเปิดใช้การสุ่มตัวอย่างฝั่งไคลเอ็นต์ในอัตรา 50% ผ่าน EasyTracker และ XML ให้ใช้พารามิเตอร์ต่อไปนี้ในไฟล์ analytics.xml
<string name="ga_sampleFrequency">50.0</string>
นอกจากนี้คุณยังเปิดใช้การสุ่มตัวอย่างฝั่งไคลเอ็นต์แบบเป็นโปรแกรมได้ด้วย โดยทำดังนี้
// Get tracker from singleton. GoogleAnalytics gaInstance = GoogleAnalytics.getInstance(this); Tracker tracker = gaInstance.getTracker("UA-XXXX-Y"); // Placeholder property ID. // Set a sample rate of 50%. tracker.setSampleRate(50.0d);
การเลือกไม่ใช้ระดับแอป
คุณสามารถเปิดใช้แฟล็กการเลือกไม่ใช้ระดับแอปซึ่งจะปิดใช้ Google Analytics ทั่วทั้งแอป เมื่อตั้งค่าแล้ว แฟล็กนั้นจะยังคงอยู่ตลอดอายุของแอปหรือจนกว่าจะรีเซ็ต
หากต้องการรับการตั้งค่าการเลือกไม่ใช้ระดับแอป ให้ใช้
// Get singleton. GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this); // Get the app opt out preference using an AppOptOutCallback. myInstance.requestAppOptOut(new AppOptOutCallback() { @Override public void reportAppOptOut(boolean optOut) { if (optOut) { ... // Alert the user that they've opted out. } }); }
หากต้องการตั้งค่าสถานะการเลือกไม่ใช้ระดับแอป ให้ใช้
myInstance.setAppOptOut(appPreferences.userOptOut);
การทดสอบและการแก้ไขข้อบกพร่อง
Google Analytics SDK สำหรับ Android มีโหมดแก้ไขข้อบกพร่องที่จะพิมพ์ข้อมูลที่เป็นประโยชน์เกี่ยวกับข้อมูลที่ส่งไปยัง Google Analytics ในบันทึก
หากต้องการเปิดใช้โหมดแก้ไขข้อบกพร่องโดยใช้ EasyTracker ให้เพิ่มค่าต่อไปนี้ลงในไฟล์ทรัพยากร analytics.xml
<bool name="ga_debug">true</bool>
หากต้องการเปิดใช้โหมดแก้ไขข้อบกพร่องแบบเป็นโปรแกรม ให้ใช้ปุ่มต่อไปนี้
// Get singleton using application context. GoogleAnalytics myInstance = GoogleAnalytics.getInstance(this); // Enable debug mode. myInstance.setDebug(true);
เอาต์พุตจะพิมพ์ไปยัง
logcat
โดยใช้แท็ก GAV2