Advanced Functionality (iOS)
|
Collect
1. Event Property Plugin
The Event Property Plugin is an advanced feature provided by the Sensors Analytics iOS SDK version v4.4.6 and later. It allows for the addition or modification of properties for buried events. With the property plugin, specific events can be filtered and new properties can be added or existing properties can be modified for those specific events.
1.1. Interface Introduction
The SDK provides the - registerPropertyPlugin: interface, which allows for the registration of custom property plugins in the initialization configuration SAConfigOptions or SDK instance SensorsAnalyticsSDK. After registering a property plugin that inherits the SAPropertyPlugin base class, properties can be added to specified events.
Introduction to the SAPropertyPlugin base class:
Method Name | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
- properties | Returns the properties collected by the plugin. NSDictionary can be used to return custom properties. | ||||||||||
- isMatchedWithFilter: | Specifies the scope of the properties in the plugin. The default value is applicable to track type events. The filter parameter that implements the SAPropertyPluginEventFilter protocol contains information that can be used for filtering. Interface of SAPropertyPluginEventFilter protocol:
| ||||||||||
- priority | The priority of the property plugin. The default is "Default" (500). Higher priority property plugins will override the same-named properties collected by lower priority plugins |
1.2. Usage Example
You can filter certain events based on event name, event type, event time, and other conditions, and add properties to these events. First, implement your own property plugin and use the - registerPropertyPlugin: interface to register the property plugin. It is recommended to register it in the initialization configuration SAConfigOptions.
Refer to the example code below:
// MyPropertyPlugin.h @interface MyPropertyPlugin : SAPropertyPlugin @end // MyPropertyPlugin.m @implementation MyPropertyPlugin - (BOOL)isMatchedWithFilter:(id<SAPropertyPluginEventFilter>)filter { return filter.type & SAEventTypeDefault; } - (SAPropertyPluginPriority)priority { return SAPropertyPluginPriorityDefault; } - (NSDictionary<NSString *,id> *)properties { return @{@"AppName": @"<#YOUR APP NAME#>"}; } @end - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { SAConfigOptions *options = [[SAConfigOptions alloc] initWithServerURL:<#数据接收地址#> launchOptions:launchOptions]; // 注册自定义属性插件 MyPropertyPlugin *propertyPlugin = [[MyPropertyPlugin alloc] init]; [options registerPropertyPlugin:propertyPlugin]; // 初始化 SDK [SensorsAnalyticsSDK startWithConfigOptions:options]; return YES; }
// MyPropertyPlugin.swift class MyPropertyPlugin: SAPropertyPlugin { override func isMatched(with filter: SAPropertyPluginEventFilter) -> Bool { return filter.type.contains(SAEventType.default) } override func priority() -> SAPropertyPluginPriority { return SAPropertyPluginPriority.default } override func properties() -> [String : Any] { return ["AppName": "YOUR APP NAME"] } } // AppDelegate.swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let options = SAConfigOptions(serverURL: <#数据接收地址#>, launchOptions: launchOptions) // 注册事件属性插件 SensorsAnalyticsSDK.start(configOptions: options) return true }
2. Intercept events before they are stored
Version SDK 1.11.16+ supports modifying properties and deleting events before they are stored. You can use the - trackEventCallback: block parameter to set it:
- The eventName parameter in the block indicates the current event name.
- In the block, the properties parameter is the current event property. This parameter is of type NSMutableDictionary, and can be directly modified to achieve the purpose of modifying event properties.
- The return value of the block indicates whether the event needs to continue to be stored and reported: YES means the event will continue to be stored, NO means the event will be deleted.
Warning
- It is strictly forbidden to call - track:, - trackInstallation:, - login: and other triggering event interfaces in the block, as it will cause the application to crash due to cyclic calling.
- If the block returns NO, the SDK will delete the event, so please ensure the judgment logic to avoid mistakenly deleting events.
[[SensorsAnalyticsSDK sharedInstance] trackEventCallback:^BOOL(NSString *eventName, NSMutableDictionary<NSString *,id> *properties) { // BuyProduct 事件不进行入库 if ([eventName isEqualToString:@"BuyProduct"]) { return NO; } // 删除 ViewProduct 事件 category 属性 if ([eventName isEqualToString:@"ViewProduct"]) { [properties removeObjectForKey:@"category"]; } return YES; }];
SensorsAnalyticsSDK.sharedInstance()?.trackEventCallback({ (eventName, properties) -> Bool in // BuyProduct 事件不进行入库 if (eventName == "BuyProduct"){ return false } // 删除 ViewProduct 事件 category 属性 if (eventName == "ViewProduct"){ properties.removeObject(forKey: "category") } return true })
3. Customize Cache Encryption
In the basic API introduction document, the SDK cache encryption supports AES encryption. If you need to use other encryption methods, you need to register a custom encryption plugin to implement it.
3.1. Interface Introduction
Custom encryption plug-in implementation <SAStorePlugin> protocol, During SDK initialization, call - registerStorePlugin: This interface can be enabled by registering the user-defined encryption plug-in.
SAStorePlugin Protocol interface introduction:
Method name | Description |
---|---|
- type | Override this method to return the plugin type. Plugin types must be unique. |
- upgradeWithOldPlugin: | Override this method to update values from old plugins when registering the plugin. This method may be called multiple times, but each time with a different plugin instance. |
- objectForKey: | Retrieve stored data based on the specified key. Implement this method. If the data is encrypted, it needs to be decrypted. |
- setObject: forKey: | Store data by implementing this method. Here you can implement custom encryption. |
- removeObjectForKey: | Remove the object stored for the specified key by implementing this method. |
When creating a custom cache encryption storage plugin, the developer needs to implement the methods for storing and retrieving data.
3.2. Usage Example
The current SDK includes the SAAESStorePlugin plugin, which uses the AES encryption algorithm to encrypt locally stored data.
SAConfigOptions *options = [[SAConfigOptions alloc] initWithServerURL:<#数据接收地址#> launchOptions:launchOptions]; [options registerStorePlugin:[[SAAESStorePlugin alloc] init]]; [SensorsAnalyticsSDK startWithConfigOptions:options];
let options = SAConfigOptions(serverURL: <#数据接收地址#>, launchOptions: launchOptions) options.register(SAAESStorePlugin()) SensorsAnalyticsSDK.start(configOptions: options)
4. Restrict SDK from reading sensitive properties
By setting the properties of the SDK externally, the SDK's automatic reading of device identification-related sensitive information can be restricted. This is used to solve compliance issues caused by the SDK reading sensitive information. Currently, the SDK caches IDFA, IDFV, and carrier name information in memory. When successfully retrieved, the cached data is used instead of making redundant calls.
设置该属性风险较高,使用前请咨询值班同学指导。
4.1. Interface Description
| Set the attribute information that needs to be limited by the SDK | Set the content of the limited attribute to replace the corresponding SDK interface call, used to avoid calling sensitive APIs by the SDK |
iOS SDK Preset LimitKey Constants
SALimitKeyIDFA | IDFA, used for collecting properties $device_id and user identification (distinct_id, AnonymousId) in anonymous mode. When IDFA is disabled, IDFV is used as the user identification. Please ensure the correctness of the value passed. |
SALimitKeyIDFV | IDFV, used for collecting properties $device_id and user identification (distinct_id, AnonymousId) in anonymous mode. When IDFV is disabled, UUID is used as the user identification. Please ensure the correctness of the value passed. |
SALimitKeyCarrier | Carrier, carrier name. Please ensure the correctness of the value passed. |
4.2. Usage Example
Attention
IDFA and IDFV involve user identification. Please ensure that the correct content is passed.
SAConfigOptions *options = [[SAConfigOptions alloc] initWithServerURL:<#数据接收地址#> launchOptions:launchOptions]; // 限制敏感 API [options registerLimitKeys:@{SALimitKeyIDFA: @"自定义 IDFA", SALimitKeyIDFV: @"自定义 IDFV", SALimitKeyCarrier: @"自定义运营商"}]; // 开启 SDK [SensorsAnalyticsSDK startWithConfigOptions:options]
Note: The content of this document is a technical document that provides details on how to use the Sensors product and does not include sales terms; the specific content of enterprise procurement products and technical services shall be subject to the commercial procurement contract.