Developers » AS2 tagging » Apple » Advanced features » Offline hits
Offline hits
- iOS
- watchOS
- tvOS
Foreword
The AT Internet SDK enables you to store, on the user’s device, any hits that couldn’t be sent due to loss of Internet connection. For this, the SDK uses Apple’s CoreData technology.
Use of this feature is subject to the subscription to an option. This feature may increase the disk space that your application occupies on a user’s device.
Setting up the storage mode
You can set up the storage management mode directly within the Tag Composer interface, or via the Tracker’s setConfig method.
Three modes are available:
- required : Hits are only stored when a loss of connection is detected.
- always : Hits are never sent. They are directly stored on the device.
- never (by default) : Hits are never stored. If a loss of connection is detected at the same time as an attempt to send the hit, the hit will be lost.
To edit the hit storage mode, add the following code to the desired area:
Store hits during loss of connection:
tracker.setOfflineMode(OfflineModeKey.required, completionHandler: nil) // Notice that the function is async
[self.tracker setConfig:@"storage" value:@"required" completionHandler:nil];
Always store hits :
tracker.setOfflineMode(OfflineModeKey.always, completionHandler: nil)
[self.tracker setConfig:@"storage" value:@"always" completionHandler:nil];
Never store hits:
tracker.setOfflineMode(OfflineModeKey.never, completionHandler: nil)
[self.tracker setConfig:@"storage" value:@"never" completionHandler:nil];
Sending stored hits
At any moment (if an Internet connection is established), you can request that stored hits be sent.
To do this, the Tracker exposes an offline property offering a send method:
This method sends all stored hits in an asynchronous manner, and deletes them if the send was successful. If the send failed, two new attempts will be made during the next hit send. If all attempts fail, the hit will be deleted from storage.
Thanks to this method, you may decide at which moment you want hits to be sent. By setting the storage mode to always, you can simply call the send method to trigger sending at the desired moment.
Sending hits in the background
You may trigger the sending of saved hits during time your application runs in the background. To do this, add the following code in your application delegate:
func applicationDidEnterBackground(_ application: UIApplication) { self.tracker.offline.dispatch() }
- (void)applicationDidEnterBackground:(UIApplication *)application { [self.tracker.offline dispatch]; }
Apple authorises actions to be taken during the 3 minutes after an application has been put in the background. If the setup enableBackgroundTask is defined as true, the SDK will then create a BackgroundTask whicih will take care of hit sending.
Recovering hits
Listing
You can recover the list of saved hits at any moment. To do this, the get method of the Offline class is available in the Tracker.
This method returns an object table Hit. This object possesses the following properties:
- url: URL of the hit
- creationDate: the date the hit was created
- retryCount: number of attempts to send the hit
- isOffline: indicates that the hit comes from storage
- type: type of hit (e.g. Screen, Touch )
First saved hit
You can recover the first hit saved via the oldest method of the Offline class:
var hits: [Hit] = ATInternet.sharedInstance.defaultTracker.offline.get()
NSArray *hits = [[[ATInternet sharedInstance] defaultTracker].offline get];
let hit: Hit? = ATInternet.sharedInstance.defaultTracker.offline.oldest()
Hit *hit = [[[ATInternet sharedInstance] defaultTracker].offline oldest];
Last saved hit
You can recover the last saved hit via the latest method of the Offline class:
let hit: Hit? = ATInternet.sharedInstance.defaultTracker.offline.latest()
Hit *hit = [[[ATInternet sharedInstance] defaultTracker].offline latest];
Counting
You may obtain the number of hits stored in the database at any moment. To do this, the Offline class exposes a count method:
let nbHit: Int = ATInternet.sharedInstance.defaultTracker.offline.count()
int nbHit = [[[ATInternet sharedInstance] defaultTracker].offline count];
Deleting hits
You can manually delete stored hits thanks to the following static methods:
delete: Deletes all stored hits
let nbDeletedHits: Int = ATInternet.sharedInstance.defaultTracker.offline.delete()
int nbDeletedHits = [[[ATInternet sharedInstance] defaultTracker].offline delete];
delete(olderThan: Int): Deletes all hits older than the indicated number of days
let nbDeletedHits: Int = ATInternet.sharedInstance.defaultTracker.offline.delete(2)
NSInteger nbDeletedHits = [[[ATInternet sharedInstance] defaultTracker].offline deleteOlderThanInt:2];
delete(olderThan: NSDate): Deletes hits older than the date passed in the parameter
let nbDeletedHits: Int = ATInternet.sharedInstance.defaultTracker.offline.delete(NSDate())
NSInteger nbDeletedHits = [[[ATInternet sharedInstance] defaultTracker].offline deleteOlderThanDate:[NSDate date]];