diff --git a/CompassSDK/Storage/CompassStorage.swift b/CompassSDK/Storage/CompassStorage.swift index c5c4d7f..84c5028 100644 --- a/CompassSDK/Storage/CompassStorage.swift +++ b/CompassSDK/Storage/CompassStorage.swift @@ -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() { @@ -68,8 +68,19 @@ class PListCompassStorage: PListStorage { } private var model: Model? { + willSet { + guard let model = model, model.hasConsent != newValue?.hasConsent, newValue?.hasConsent == false else { + return + } + + remove(filename: Store.v2.rawValue) + } 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) } } diff --git a/CompassSDK/Storage/PListStorage.swift b/CompassSDK/Storage/PListStorage.swift index 900ddc7..617c154 100644 --- a/CompassSDK/Storage/PListStorage.swift +++ b/CompassSDK/Storage/PListStorage.swift @@ -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 { @@ -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 {} + } } diff --git a/Playground/Playground/AllPostsView.swift b/Playground/Playground/AllPostsView.swift index 0f2f2d9..aae1ece 100644 --- a/Playground/Playground/AllPostsView.swift +++ b/Playground/Playground/AllPostsView.swift @@ -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") }) } diff --git a/Playground/Playground/BlogPostView.swift b/Playground/Playground/BlogPostView.swift index cf61c6f..f274b59 100644 --- a/Playground/Playground/BlogPostView.swift +++ b/Playground/Playground/BlogPostView.swift @@ -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 {