Problem
On iOS we are using the latest version of Google Analytics SDK integrated via Cocoapods. Recently we switched to Manual session management as explained in their SDK docs On looking into the GA dashboard we noticed that there is a huge drop in avg. session time. This is because now we are seeing majority of sessions with length in 1 to 10 seconds range.
Another thing we noticed is that there are more number of "Session Start" screen views than "Session End". See startAppSession method below which is used to start/end a session with a screenView hit.
Background Fetch
We do have background fetch in our application. Is this could be the problem? We ruled out it as it use to be same when we had SDKs automatic session management/
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
Session time out setting
The session time out setting in our dashboard = 30 minutes.
Code sample for reference
Method to initialise the Google iOS SDK.
func setup() {
let gai = GAI.sharedInstance()
gai.dispatchInterval = 30
let customDimensionOS = GAIFields.customDimension(for: 1)
let tracker = gai.tracker(withName: "master", trackingId: masterTrackingId)!
tracker.set(customDimensionOS, value: deviceOS())
}
Method to start/end a session.
func startAppSession(start:Bool) {
let builder = GAIDictionaryBuilder.createScreenView()!
tracker.set(kGAISessionControl, value: start ? "start" : "end")
tracker.set(kGAIScreenName, value: start ? "Session Start" : "Session End")
tracker.send(builder.build() as [NSObject : AnyObject])
tracker.set(kGAISessionControl, value: nil)
}
Start a session From didFinishLaunchingWithOptions & applicationDidEnterBackground
func onApplicationWillEnterForegroundNotification(_:Notification) {
if let gai = GAI.sharedInstance() {
gai.dispatchInterval = 30
}
sendAppSession(start: true)
}
}
End a session From applicationWillEnterForeground
func onApplicationDidEnterBackgroundNotification(_:Notification) {
sharedInstance.backgroundTask = UIApplication.shared.beginBackgroundTask(withName: "Analytics.EndTimedEvents") {
UIApplication.shared.endBackgroundTask(sharedInstance.backgroundTask)
sharedInstance.backgroundTask = UIBackgroundTaskInvalid
}
DispatchQueue.global().async {
sendAppSession(start: false)
UIApplication.shared.endBackgroundTask(sharedInstance.backgroundTask)
sharedInstance.backgroundTask = UIBackgroundTaskInvalid
GADispatch()
}
}
Some relevant (but not exact) SO questions
Google Analytics iOS SDK “1 second sessions” (possibly background sessions?)
Session control with Google Analytics API v3 for iOS?
Google Issue tracker