Developers » AS2 tagging » Apple » Fonctionnalités avancées » Envoi des hits en arrière-plan
Envoi des hits en arrière-plan
- iOS
- watchOS
- tvOS
Intérêt de l’identification
L’identification vous permet de distinguer les hits envoyés suite à l’utilisation directe de votre application, des hits envoyés automatiquement et indépendamment de l’utilisateur en arrière-plan, à l’aide du background fetch ou d’une background task par exemple.
Marquage
Le tracker possède une propriété context qui possède une sous propriété backgroundMode. C’est une énumération de type BackgroundMode.
Par défaut, cette propriété est positionnée à Normal. Dans le cas d’un marquage du background fetch, il faut positionner la propriété à Fetch, et dans tous les autres cas de marquage d’un comportement automatique et indépendant de l’utilisateur, il faut la positionner à Task.
Pour marquer ces types de fonctionnement, il est recommandé d’utiliser une instance de tracker dédiée. Si vous souhaitez utiliser une seule instance de tracker, il faut alors penser à repositionner la propriété à Normal à la fin de l’exécution du background fetch ou de la background task.
Exemples de marquage
- Marquage du background fetch avec un tracker dédiéd
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
- Marquage du background fetch avec un tracker global
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; }
- Marquage d’une background task avec un tracker dédié
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
- Marquage d’une background task avec un tracker global
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; }); }