Developers » AS2 tagging » Apple » Advanced features » Sending background hits
Sending background hits
- iOS
- watchOS
- tvOS
Utility of identification
Identification enables you to distinguish hits sent following direct usage of your application from hits sent in the background, automatically and independently of the user, with the help of background fetch or a background task for example.
Tagging
The tracker possesses a context property, which possesses a subproperty, backgroundMode. This is BackgroundMode-type enumeration.
By default, this property is set to Normal. In the case of background fetch tagging, the property must be set to Fetch, and in all other cases of tagging automatic behaviours independent of the user, it should be set to Task.
To tag these types of functioning, it is recommended to use an instance of a dedicated tracker. If you wish to use just one tracker instance, you must remember to reset the property to Normal at the end of execution of background fetch or background task.
Tagging examples
- Tagging background fetch with a dedicated tracker
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let tracker = ATInternet.sharedInstance.tracker("bgFetchTracker") func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionKey: Any]?) -> Bool { tracker.context.backgroundMode = BackgroundMode.fetch return true } func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // Do the work associated with the fetch tracker.gestures.add("fetch").sendDownload() } }
#import "SmartTracker/SmartTracker-Swift.h" @interface AppDelegate () @property (nonatomic, strong) Tracker *tracker; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tracker = [[ATInternet sharedInstance] trackerWithName:@"bgFetchTracker"]; self.tracker.context.backgroundMode = BackgroundModeFetch; return YES; } - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Do the work associated with the fetch [[self.tracker.gestures add:@"fetch"] sendDownload]; } @end
- Tagging background fetch with a global tracker
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { // Do the work associated with the fetch let tracker = ATInternet.sharedInstance.defaultTracker tracker.context.backgroundMode = BackgroundMode.fetch tracker.gestures.add("fetch").sendDownload() tracker.context.backgroundMode = BackgroundMode.normal }
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Do the work associated with the fetch Tracker *tracker = [ATInternet sharedInstance].defaultTracker; tracker.context.backgroundMode = BackgroundModeFetch; [[tracker.gestures addWithName:@"fetch"] sendDownload]; tracker.context.backgroundMode = BackgroundModeNormal; }
- Tagging a background task with a dedicated tracker
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let tracker = ATInternet.sharedInstance.tracker("bgTaskTracker") var task = UIBackgroundTaskInvalid func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { tracker.context.backgroundMode = BackgroundMode.task return true } func applicationDidEnterBackground(application: UIApplication) { self.task = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in application.endBackgroundTask(self.task) self.task = UIBackgroundTaskInvalid } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in // Do the work associated with the task self.tracker.gestures.add("task").sendDownload() application.endBackgroundTask(self.task) self.task = UIBackgroundTaskInvalid }) } }
#import "SmartTracker/SmartTracker-Swift.h" @interface AppDelegate () @property (nonatomic, strong) Tracker *tracker; @property (nonatomic) UIBackgroundTaskIdentifier task; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.tracker = [[ATInternet sharedInstance] trackerWithName:@"bgTaskTracker"]; self.tracker.context.backgroundMode = ATBackgroundModeTask; self.task = UIBackgroundTaskInvalid; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { self.task = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ [application endBackgroundTask:self.task]; self.task = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task [[self.tracker.gestures add:@"task"] sendDownload]; [application endBackgroundTask:self.task]; self.task = UIBackgroundTaskInvalid; }); } @end
- Tagging a background task with a global tracker
func applicationDidEnterBackground(_ application: UIApplication) { let tracker = ATInternet.sharedInstance.defaultTracker tracker.context.backgroundMode = BackgroundMode.task self.task = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in tracker.context.backgroundMode = BackgroundMode.normal application.endBackgroundTask(self.task) self.task = UIBackgroundTaskInvalid } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in // Do the work associated with the task tracker.gestures.add("task").sendDownload() tracker.context.backgroundMode = BackgroundMode.normal application.endBackgroundTask(self.task) self.task = UIBackgroundTaskInvalid }) }
- (void)applicationDidEnterBackground:(UIApplication *)application { Tracker *tracker = [ATInternet sharedInstance].defaultTracker; tracker.context.backgroundMode = BackgroundModeTask; self.task = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ tracker.context.backgroundMode = BackgroundModeNormal; [application endBackgroundTask:self.task]; self.task = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task [[tracker.gestures add:@"task"] sendDownload]; tracker.context.backgroundMode = BackgroundModeNormal; [application endBackgroundTask:self.task]; self.task = UIBackgroundTaskInvalid; }); }