diff --git a/Example/iOS Example/AppDelegate.swift b/Example/iOS Example/AppDelegate.swift index 2b993e74..a4f3b924 100644 --- a/Example/iOS Example/AppDelegate.swift +++ b/Example/iOS Example/AppDelegate.swift @@ -19,10 +19,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + Tracker.shared?.dispatch() } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + Tracker.shared?.startNewSession() } func applicationDidBecomeActive(_ application: UIApplication) { diff --git a/Example/iOS Example/ConfigurationViewController.swift b/Example/iOS Example/ConfigurationViewController.swift index cac7d084..cc9153e3 100644 --- a/Example/iOS Example/ConfigurationViewController.swift +++ b/Example/iOS Example/ConfigurationViewController.swift @@ -27,6 +27,7 @@ class ConfigurationViewController: UIViewController { } @IBAction func newSessionButtonTapped(_ sender: UIButton) { + Tracker.shared?.startNewSession() } @IBAction func dispatchButtonTapped(_ sender: UIButton) { diff --git a/PiwikTracker/Tracker.swift b/PiwikTracker/Tracker.swift index 33ba0c70..0c69a4c9 100644 --- a/PiwikTracker/Tracker.swift +++ b/PiwikTracker/Tracker.swift @@ -24,6 +24,7 @@ final public class Tracker: NSObject { self.queue = queue self.dispatcher = dispatcher super.init() + startNewSession() startDispatchTimer() } @@ -43,6 +44,7 @@ final public class Tracker: NSObject { internal func queue(event: Event) { queue.enqueue(event: event) + nextEventStartsANewSession = false } // MARK: dispatching @@ -51,7 +53,8 @@ final public class Tracker: NSObject { private(set) var isDispatching = false - /// Manually start the dispatching process. + /// Manually start the dispatching process. You might want to call this method in AppDelegates `applicationDidEnterBackground` to transmit all data + /// whenever the user leaves the application. public func dispatch() { guard !isDispatching else { return } guard queue.eventCount > 0 else { @@ -95,6 +98,20 @@ final public class Tracker: NSObject { internal var visitor = Visitor.current() internal var session = Session.current() + internal var nextEventStartsANewSession = true +} + +extension Tracker { + /// Starts a new Session + /// + /// Use this function to manually start a new Session. A new Session will be automatically created only on app start. + /// You can use the AppDelegates `applicationWillEnterForeground` to start a new visit whenever the app enters foreground. + public func startNewSession() { + PiwikUserDefaults.standard.previousVisit = PiwikUserDefaults.standard.currentVisit + PiwikUserDefaults.standard.currentVisit = Date() + PiwikUserDefaults.standard.totalNumberOfVisits += 1 + self.session = Session.current() + } } // shared instance @@ -142,7 +159,7 @@ extension Tracker { url: URL(string: "http://example.com")!.appendingPathComponent(action.joined(separator: "/")), actionName: action, language: Locale.httpAcceptLanguage, - isNewSession: false, // set this to true once we can start a new session + isNewSession: nextEventStartsANewSession, referer: nil, eventCategory: nil, eventAction: nil, @@ -156,12 +173,11 @@ extension Tracker { uuid: NSUUID(), visitor: visitor, session: session, - date: Date(), url: URL(string: "http://example.com")!, actionName: [], language: Locale.httpAcceptLanguage, - isNewSession: false, // set this to true once we can start a new session + isNewSession: nextEventStartsANewSession, referer: nil, eventCategory: category, eventAction: action, diff --git a/Podfile.lock b/Podfile.lock index 5db08b34..3d804a4d 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -1,8 +1,8 @@ PODS: - Nimble (5.1.1) - - PiwikTracker (4.0.0-alpha1): - - PiwikTracker/Core (= 4.0.0-alpha1) - - PiwikTracker/Core (4.0.0-alpha1) + - PiwikTracker (4.0.0-beta1): + - PiwikTracker/Core (= 4.0.0-beta1) + - PiwikTracker/Core (4.0.0-beta1) - Quick (0.10.0) DEPENDENCIES: @@ -16,7 +16,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Nimble: 415e3aa3267e7bc2c96b05fa814ddea7bb686a29 - PiwikTracker: 6a8cebd3663430091db323dd93047be254e77bc3 + PiwikTracker: 9a2fb2fe96ff5330719c05d7f77e73a9ed4b2237 Quick: 5d290df1c69d5ee2f0729956dcf0fd9a30447eaa PODFILE CHECKSUM: a7f24eb0ec9a2aaa14dbc693f87f65c7b3ca1934