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

fix: do not persist if no consent #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions CompassSDK/Storage/CompassStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PListCompassStorage: PListStorage {
var userSegments: [String]?
var hasConsent: Bool?

static var empty: Model {.init(numVisits: 0, userId: nil, suid: nil, firstVisit: nil, lastVisit: nil, userVars: Vars(), sessionVars: Vars(), userSegments: [])}
static var empty: Model {.init(numVisits: 0, userId: nil, suid: nil, firstVisit: nil, lastVisit: nil, userVars: Vars(), sessionVars: Vars(), userSegments: [], hasConsent: nil)}
}

init() {
Expand All @@ -68,8 +68,19 @@ class PListCompassStorage: PListStorage {
}

private var model: Model? {
willSet {
guard let model = model, model.hasConsent != newValue?.hasConsent, newValue?.hasConsent == false else {

Choose a reason for hiding this comment

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

why do you need to do the let model = model? can't you use it directly?

return
}

remove(filename: Store.v2.rawValue)
Copy link
Author

Choose a reason for hiding this comment

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

remove stored data if consent has changed to false

}
didSet {
guard let model = model else {return}
guard let model = model, model.hasConsent == true || model.hasConsent == nil else {
return

}

persist(filename: Store.v2.rawValue, values: model)
}
}
Expand Down
12 changes: 12 additions & 0 deletions CompassSDK/Storage/PListStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protocol PListStorage {
var fileManager: FileManager {get}
func load(_ filename: String) -> Model?
func persist(filename: String, values: Model)
func remove(filename: String)
}

extension PListStorage {
Expand All @@ -33,4 +34,15 @@ extension PListStorage {

try? data.write(to: path)
}

func remove(filename: String) {
guard let path = getFilePath(filename) else {
return
}

do {
try fileManager.removeItem(at: path)

} catch {}
}
}
3 changes: 1 addition & 2 deletions Playground/Playground/AllPostsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ struct AllPosts: View {
}
.onAppear(perform: {
CompassTracker.shared.setUserType(.logged)
CompassTracker.shared.setConsent(false)
CompassTracker.shared.addUserSegment("segment1")
CompassTracker.shared.addUserSegment("segment1")
CompassTracker.shared.addUserSegment("segment2")
CompassTracker.shared.setSessionVar(name: "lolo", value: "lola")
CompassTracker.shared.setSessionVar(name: "lolo2", value: "lola2")
CompassTracker.shared.setUserVar(name: "hihi", value: "haha")
CompassTracker.shared.setUserVar(name: "hihi2", value: "haha2")
CompassTracker.shared.setUserVar(name: "hihi2", value: "hahaha2")
CompassTracker.shared.trackScreen("ios homepage")
})
}
Expand Down
1 change: 1 addition & 0 deletions Playground/Playground/BlogPostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct BlogPostView: View {
CompassTracker.shared.trackNewPage(url: URL(string: blogPost.url)!)
CompassTracker.shared.setPageVar(name: "pepe", value: blogPost.id.description)
CompassTracker.shared.setPageVar(name: "pepe2", value: blogPost.id.description)
CompassTracker.shared.setUserVar(name: "pepe-user", value: blogPost.id.description)
})
.onReceive(videoPlayer.playbackStatePublisher) { state in
guard isVideoInitialized else {
Expand Down