Programmatically Control Sessions
By default, a mobile session ends after a period of user inactivity. For example, when a user opens your application, the session begins and only ends after the user stops using the app for a set period of time. When the user begins to use the application again, a new session begins.
Instead of having a period of inactivity to define the duration of a session, however, you can use the following API to programmatically control when sessions begin and end:
- (void)startNextSession
When you call the method startNextSession from the ADEumInstrumentation class, the current session ends and a new session begins. The API enables you to define and frame your sessions so that they align more closely with business goals and expected user flows. For example, you could use the API to define a session that tracks a purchase of a product or registers a new user.
Excessive use of this API will cause sessions to be throttled (excessive use is >10 calls per minute per iOS Agent, but is subject to change). When not using the API, sessions will fall back to the default of ending after a period of user inactivity.
Example of a Programmatically Controlled Session
- Objective-C
-
JAVASCRIPT
-(void) checkout { AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate]; NSString *checkoutUrl = [appDelegate.url stringByAppendingString:@"rest/cart/co/"]; NSURL *url = [NSURL URLWithString:checkoutUrl]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSURLResponse *response = nil; NSError *error = nil; NSData *body = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; const char *responseBytes = [body bytes]; if (responseBytes == nil) checkoutResponse = [NSString stringWithUTF8String:"Could not connect to the server"]; else { checkoutResponse = [NSString stringWithUTF8String:responseBytes]; [ADEumInstrumentation startNextSession]; } } - Swift
-
JAVASCRIPT
func checkout() { let appDelegate = UIApplication.shared.delegate as? AppDelegate let checkoutUrl = appDelegate?.url ?? "" + ("rest/cart/co/") let url = URL(string: checkoutUrl) var request: NSMutableURLRequest? = nil if let url = url { request = NSMutableURLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60.0) } var response: URLResponse? = nil var error: Error? = nil var body: Data? = nil if let request = request { body = try? NSURLConnection.sendSynchronousRequest(request, returning: &response) } let responseBytes = Int8(body?.bytes ?? 0) if responseBytes == nil { checkoutResponse = String(utf8String: "Could not connect to the server") } else { checkoutResponse = String(utf8String: &responseBytes) ADEumInstrumentation.startNextSession() } }