Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/136 starting sessions #141

Merged
merged 2 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Example/iOS Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Example/iOS Example/ConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ConfigurationViewController: UIViewController {
}

@IBAction func newSessionButtonTapped(_ sender: UIButton) {
Tracker.shared?.startNewSession()
}

@IBAction func dispatchButtonTapped(_ sender: UIButton) {
Expand Down
24 changes: 20 additions & 4 deletions PiwikTracker/Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final public class Tracker: NSObject {
self.queue = queue
self.dispatcher = dispatcher
super.init()
startNewSession()
startDispatchTimer()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of startNewSession() also calling startDispatchTimer()? Are there cases where a new session should not start a new timer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call startDispatchTimer() directly, rather dispatch(). dispatch() would dispatch the currently stored events and starts a new timer.

In this case I don't think doing any of those is what we want. Starting a new session is not necessary coupled to dispatching events. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that.

}

Expand All @@ -43,6 +44,7 @@ final public class Tracker: NSObject {

internal func queue(event: Event) {
queue.enqueue(event: event)
nextEventStartsANewSession = false
}

// MARK: dispatching
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -16,7 +16,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Nimble: 415e3aa3267e7bc2c96b05fa814ddea7bb686a29
PiwikTracker: 6a8cebd3663430091db323dd93047be254e77bc3
PiwikTracker: 9a2fb2fe96ff5330719c05d7f77e73a9ed4b2237
Quick: 5d290df1c69d5ee2f0729956dcf0fd9a30447eaa

PODFILE CHECKSUM: a7f24eb0ec9a2aaa14dbc693f87f65c7b3ca1934
Expand Down