From version 6.0.2(22), the timestamp and serviceTag attributes are added to the API for registering conversion events. The conversion event occurrence time and the business information followed by developers can be set.
The app attribution service enables media apps and distribution platforms to register attribution sources (for example, an impression or a click), and enables developers' apps to register conversion events, both with the device-side app attribution service.
According to system rules, the device-side app attribution service completes attribution calculation based on the attribution sources registered by media apps and conversion events registered by developers' apps.
The app attribution service provides the following APIs. For details, please refer to the API reference.
| API | Description |
|---|---|
| registerSource(adSourceInfo: AdSourceInfo): Promise<void> | Registers attribution sources. |
| registerTrigger(adTriggerInfo: AdTriggerInfo): Promise<void> | Registers conversion events. |
Import required modules.
import { BusinessError, deviceInfo } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { SignUtil } from '../common/utils/SignUtil';
import { util } from '@kit.ArkTS';
import { attributionManager } from '@kit.AppGalleryKit'; Construct the AdSourceInfo parameter.
// Private key, which is generated by developers and used to generate a signature.
let privateKey: string = '********u2rMtFw==';
// Attribution role ID of a distribution platform, which is allocated by the app attribution service during role registration on the cloud platform of the app attribution service.
let adTechId: string = '2******';
// ID of the ad task created by the ad platform.
// Starting from API version 12, the value of campaignId cannot exceed six characters.
// Starting from API version 22, the value of campaignId cannot exceed nine characters.
let campaignId: string = '';
let osApiVersion: number = deviceInfo.sdkApiVersion;
if (osApiVersion >= 22) {
campaignId = '1*******9';
} else {
campaignId = '1****6';
}
// ID of the developer's app released on AppGallery, without letter C.
let destinationId: string = '6*********4';
// ID of the attribution monitoring platform.
let mmpIds: string[] = ['2f*****15'];
// Business information relevant to the distribution platform.
let serviceTag: string = 'testServiceTag';
// Random string without hyphens (-), used for signature calculations.
let nonce: string = util.generateRandomUUID().replace(/-/g, '');
// Timestamp.
let timestamp: number = Date.now()
let adSourceInfo: attributionManager.AdSourceInfo = {
adTechId: adTechId,
campaignId: campaignId,
destinationId: destinationId,
// Impression.
sourceType: attributionManager.SourceType.IMPRESSION,
mmpIds: mmpIds,
serviceTag: serviceTag,
nonce: nonce,
timestamp: timestamp,
// Signature.
signature: await SignUtil.getSign(this.getUIContext(),
SignUtil.genSignContent(adTechId, campaignId, destinationId, mmpIds, serviceTag, nonce, timestamp),
privateKey)
}; Call the attributionManager.registerSource method to register attribution sources.
try {
// ...
attributionManager.registerSource(adSourceInfo).then(() => {
hilog.info(0, TAG, 'registerSource success.');
// ...
}).catch((error: BusinessError) => {
hilog.error(0, TAG, `registerSource error. code is ${error.code}, message is ${error.message}`);
// ...
})
} catch (error) {
hilog.error(0, TAG, `registerSource error. code is ${error.code}, message is ${error.message}`);
// ...
} Import required modules.
import { BusinessError, deviceInfo } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// ...
import { attributionManager } from '@kit.AppGalleryKit'; Construct the AdTriggerInfo parameter.
let adTriggerInfo: attributionManager.AdTriggerInfo = {
// Conversion event ID, which is obtained from the cloud platform of the app attribution service.
triggerData: 123,
businessScene: 5
};
let osApiVersion: number = deviceInfo.sdkApiVersion;
if (osApiVersion >= 22) {
// The parameter indicating the actual occurrence time is added starting from API version 22.
adTriggerInfo.timestamp = Date.now();
// Business information relevant to the ad platform is added starting from API version 22.
adTriggerInfo.serviceTag = 'testServiceTag';
} Call the attributionManager.registerTrigger method to register conversion events.
try {
// ...
attributionManager.registerTrigger(adTriggerInfo).then(() => {
hilog.info(0, TAG, 'registerTrigger success');
// ...
}).catch((error: BusinessError) => {
hilog.error(0, TAG, `registerTrigger error. code is ${error.code}, message is ${error.message}`);
// ...
})
} catch (error) {
hilog.error(0, TAG, `registerTrigger error. code is ${error.code}, message is ${error.message}`);
// ...
}