importFirebaseAnalytics// ...// If you're using an email address....Analytics.initiateOnDeviceConversionMeasurement(emailAddress:"example@gmail.com")// If you're using a phone number....Analytics.initiateOnDeviceConversionMeasurement(phoneNumber:"+15555555555")
@importFirebaseAnalytics;// ...// If you're using an email address....[FIRAnalyticsinitiateOnDeviceConversionMeasurementWithEmailAddress:@"example@gmail.com"];// If you're using a phone number....[FIRAnalyticsinitiateOnDeviceConversionMeasurementWithPhoneNumber:@"+15555555555"];
usingFirebase.Analytics;// ...// If you're using an email address....FirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithEmailAddress("example@gmail.com");// If you're using a phone number....FirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithPhoneNumber("+15555555555");
使用經過雜湊處理的電子郵件地址或電話號碼
API 會接受使用 SHA256 雜湊處理的電子郵件地址和電話號碼。您可以在呼叫 SDK 之前,在程式碼中執行雜湊運算,藉此控管使用者資料。
如要使用雜湊憑證,請將地址和號碼標準化,並使用 SHA256 雜湊處理,然後呼叫 API。
將電子郵件地址和電話號碼正規化
針對電子郵件地址,Google Analytics API 會假設在套用 SHA256 之前,會先執行特定的規格化作業,因此請按照下列步驟將資料規格化:
importFirebaseAnalytics// ...// If you're using an email address....Analytics.initiateOnDeviceConversionMeasurement(hashedEmailAddress:hashedEmailAddress)// If you're using a phone number....Analytics.initiateOnDeviceConversionMeasurement(hashedPhoneNumber:hashedPhoneNumber)
@importFirebaseAnalytics;// ...// If you're using an email address....[FIRAnalyticsinitiateOnDeviceConversionMeasurementWithHashedEmailAddress:hashedEmailAddress];// If you're using a phone number....[FIRAnalyticsinitiateOnDeviceConversionMeasurementWithHashedPhoneNumber:hashedPhoneNumber];
usingFirebase.Analytics;// ...// If you're using an email address....FirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithHashedEmailAddress(hashedEmailAddress);// If you're using a phone number....FirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber(hashedPhoneNumber);
使用 Xcode 偵錯記錄進行驗證 (選用)
如果您啟用了偵錯模式,請在呼叫啟動評估 API 後,確認 Xcode 偵錯主控台中會顯示類似下列記錄訊息的訊息:
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["Step 3: Initiate on-device conversion measurement using Google Analytics\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------|\n| Introduction: [Measure iOS Ads conversions](/docs/tutorials/ads-ios-on-device-measurement/index-first-party) |\n| Step 1: [Implement a sign-in experience](/docs/tutorials/ads-ios-on-device-measurement/step-1) \u003cbr /\u003e |\n| [Step 2: Integrate Google Analytics](/docs/tutorials/ads-ios-on-device-measurement/step-2) |\n| **Step 3: Initiate on-device conversion measurement using Google Analytics** \u003cbr /\u003e |\n| Step 4: [Troubleshoot and handle common issues](/docs/tutorials/ads-ios-on-device-measurement/step-4) |\n\n\u003cbr /\u003e\n\nNow that you can collect users' email addresses and phone numbers and your app has the\nGoogle Analytics for Firebase SDK, you can use the two to start\nmeasuring conversions.\n\nCall the API\n\nCall the conversion measurement API with the consented email address or phone\nnumber from Step 1, which is used for ads conversion measurement, without\nallowing any personally identifiable information to leave the user device.\n\nThere are two ways to initiate measurement:\n\n- [Using credentials](#use-plain-text-credentials)\n- [Using hashed credentials](#use-hashed-credentials)\n\nUse email address or phone number \n\nSwift\n\nImport the `FirebaseAnalytics` module and pass in the email address or phone\nnumber to the `initiateOnDeviceConversionMeasurement()` API.\n**Note:** The `initiateOnDeviceConversionMeasurement(emailAddress:)` and `initiateOnDeviceConversionMeasurement(phoneNumber:)` methods may be called sequentially, but only one match can be reported per user. If a match is found with either API, then subsequent API call are effectively ignored. \n\n```swift\nimport FirebaseAnalytics\n\n// ...\n// If you're using an email address....\nAnalytics.initiateOnDeviceConversionMeasurement(emailAddress: \"example@gmail.com\")\n// If you're using a phone number....\nAnalytics.initiateOnDeviceConversionMeasurement(phoneNumber: \"+15555555555\")\n```\n\nObjective-C\n\nImport the `FirebaseAnalytics` module and pass in the email address to the\n`initiateOnDeviceConversionMeasurementWithEmailAddress:` API or the phone\nnumber to the `initiateOnDeviceConversionMeasurementWithPhoneNumber:` API.\n**Note:** The `initiateOnDeviceConversionMeasurementWithEmailAddress:` and `initiateOnDeviceConversionMeasurementWithPhoneNumber:` methods may be called sequentially, but only one match can be reported per user. If a match is found with either API, then subsequent API call are effectively ignored. \n\n```objective-c\n@import FirebaseAnalytics;\n\n// ...\n// If you're using an email address....\n[FIRAnalytics initiateOnDeviceConversionMeasurementWithEmailAddress:@\"example@gmail.com\"];\n// If you're using a phone number....\n[FIRAnalytics initiateOnDeviceConversionMeasurementWithPhoneNumber:@\"+15555555555\"];\n```\n\nUnity\n\nImport the `Firebase.Analytics` namespace and pass in the email address to the\n`InitiateOnDeviceConversionMeasurementWithEmailAddress()` API or the phone\nnumber to the `InitiateOnDeviceConversionMeasurementWithPhoneNumber()` API:\n**Note:** The `InitiateOnDeviceConversionMeasurementWithEmailAddress()` and `InitiateOnDeviceConversionMeasurementWithPhoneNumber()` methods may be called sequentially, but only one match can be reported per user. If a match is found with either API, then subsequent API call are effectively ignored. \n\n```c#\nusing Firebase.Analytics;\n\n// ...\n// If you're using an email address....\nFirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithEmailAddress(\"example@gmail.com\");\n// If you're using a phone number....\nFirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithPhoneNumber(\"+15555555555\");\n```\n| **Note:** The best practice is to call the API once per install and as close as possible to the login.\n| **Important:** Be aware that if an intended in-app action or event happens *immediately* after a user email or phone registration, consider implementing a small time delay between initiating on-device conversion measurement and logging the event.\n\nUse a hashed email address or phone number\n\nThe API will accept email addresses and phone numbers hashed with SHA256. You\ncan maintain control of your user's data by performing the hashing in your code\nbefore making calls to the SDK.\n\nTo use hashed credentials, normalize addresses and numbers, hash them with\nSHA256, then call the API.\n\nNormalize email addresses and phone numbers\n\nFor **email addresses** , the Google Analytics API assumes that a\nparticular normalization is performed before SHA256 is applied, so follow these\nsteps to normalize your data:\n\n1. Convert the entire email address to lowercase.\n\n2. If the email address ends in domain *@googlemail.com* , replace the\n *@googlemail.com* domain with *@gmail.com*.\n\n3. For addresses ending in domain *@gmail.com* (including those modified in\n the previous step):\n\n 1. Remove all periods from the username portion.\n\n 2. Make the following substitutions in the username portion:\n\n - For letters I or i, or digit 1, substitute letter l\n - For digit 0, substitute letter o\n - For digit 2, substitute letter z\n - For digit 5, substitute letter s\n\nFor example, after normalization:\n\n- `an.email.user0125@googlemail.com` becomes `anemalluserolzs@gmail.com`\n- `CAPSUSER0125@provider.net` becomes `capsuser0125@provider.net`\n\nFor **phone numbers**, numbers must already be in E.164 format (that is,\nprefix with +, 1-3 digits for country code, max 12 digits for subscriber number)\nprior to hashing with SHA256.\n\nNote that the hashed SHA256 email or phone numbers must be 32 bytes long\nand not a hexadecimal string.\n\nCall the API with hashed credentials \n\nSwift\n\nImport the `FirebaseAnalytics` module and pass in the email address or phone\nnumber to the `initiateOnDeviceConversionMeasurement()` API.\n**Note:** The `initiateOnDeviceConversionMeasurement(hashedEmailAddress:)` and `initiateOnDeviceConversionMeasurement(hashedPhoneNumber:)` methods may be called sequentially, but only one match can be reported per user. If a match is found with either API, then subsequent API call are effectively ignored. \n\n```swift\nimport FirebaseAnalytics\n\n// ...\n// If you're using an email address....\nAnalytics.initiateOnDeviceConversionMeasurement(hashedEmailAddress: hashedEmailAddress)\n// If you're using a phone number....\nAnalytics.initiateOnDeviceConversionMeasurement(hashedPhoneNumber: hashedPhoneNumber)\n```\n\nObjective-C\n\nImport the `FirebaseAnalytics` module and pass in the email address to the\n`initiateOnDeviceConversionMeasurementWithHashedEmailAddress:` API or the phone\nnumber to the `initiateOnDeviceConversionMeasurementWithHashedPhoneNumber:`\nAPI.\n**Note:** The `initiateOnDeviceConversionMeasurementWithHashedEmailAddress:` and `initiateOnDeviceConversionMeasurementWithHashedPhoneNumber:` methods may be called sequentially, but only one match can be reported per user. If a match is found with either API, then subsequent API call are effectively ignored. \n\n```objective-c\n@import FirebaseAnalytics;\n\n// ...\n// If you're using an email address....\n[FIRAnalytics initiateOnDeviceConversionMeasurementWithHashedEmailAddress:hashedEmailAddress];\n// If you're using a phone number....\n[FIRAnalytics initiateOnDeviceConversionMeasurementWithHashedPhoneNumber:hashedPhoneNumber];\n```\n\nUnity\n\nImport the `Firebase.Analytics` namespace and pass in the email address to the\n`InitiateOnDeviceConversionMeasurementWithHashedEmailAddress()` API or the\nphone number to the `InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber()` API:\n**Note:** The `InitiateOnDeviceConversionMeasurementWithHashedEmailAddress()` and `InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber()` methods may be called sequentially, but only one match can be reported per user. If a match is found with either API, then subsequent API call are effectively ignored. \n\n```c#\nusing Firebase.Analytics;\n\n// ...\n// If you're using an email address....\nFirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithHashedEmailAddress(hashedEmailAddress);\n// If you're using a phone number....\nFirebaseAnalytics.InitiateOnDeviceConversionMeasurementWithHashedPhoneNumber(hashedPhoneNumber);\n```\n| **Note:** The best practice is to call the API once per install and as close as possible to the login.\n| **Important:** Be aware that if an intended in-app action or event happens *immediately* after a user email or phone registration, consider implementing a small time delay between initiating on-device conversion measurement and logging the event.\n\nVerify with Xcode debug logs (optional)\n\nIf you enabled debug mode, after calling the initiate measurement API, ensure\nthat a message like the following log message appears in the Xcode debug\nconsole: \n\n [FirebaseAnalytics][I-ACS023225] Initiated on-device conversion measurement\n\nIf you enabled debug mode and included the `-DebugOnDeviceConversionMeasurement`\nlaunch argument, then calling the `initiateOnDeviceConversionMeasurement()` API\nwill simulate a match. \n\n [FirebaseAnalytics][I-ACS023229] On-device conversion measurement found a match\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\n\u003cbr /\u003e\n\n[arrow_back_ios**Step 2** : Integrate Google Analytics](/docs/tutorials/ads-ios-on-device-measurement/step-2)\n[**Step 4** : Troubleshoot and handle common issuesarrow_forward_ios](/docs/tutorials/ads-ios-on-device-measurement/step-4)\n\n\u003cbr /\u003e\n\n*** ** * ** ***"]]