-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/Issue URL: https://app.asana.com/0/72649045549333/1206057769488160/f Description: Add iOS NetP pixels for active/new user, VPN enabled success rate, tunnel failure/recovery, and latency. Steps to test this PR: Confirm pixel submission on Kibana Make sure active user pixel includes the cohort value
- Loading branch information
1 parent
ad9ab42
commit 8697128
Showing
5 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// UniquePixel.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A variant of pixel that is fired just once. Ever. | ||
/// | ||
/// The 'fire' method mimics standard Pixel API. | ||
/// The 'onComplete' closure is always called - even when no pixel is fired. | ||
/// In those scenarios a 'UniquePixelError' is returned denoting the reason. | ||
/// | ||
public final class UniquePixel { | ||
|
||
public enum Error: Swift.Error { | ||
|
||
case alreadyFired | ||
|
||
} | ||
|
||
private enum Constant { | ||
|
||
static let uniquePixelStorageIdentifier = "com.duckduckgo.unique.pixel.storage" | ||
|
||
} | ||
|
||
public static let storage = UserDefaults(suiteName: Constant.uniquePixelStorageIdentifier)! | ||
|
||
/// Sends a unique Pixel | ||
/// This requires the pixel name to end with `_u` | ||
public static func fire(pixel: Pixel.Event, | ||
withAdditionalParameters params: [String: String] = [:], | ||
onComplete: @escaping (Swift.Error?) -> Void = { _ in }) { | ||
guard pixel.name.hasSuffix("_u") else { | ||
assertionFailure("Unique pixel: must end with _u") | ||
return | ||
} | ||
|
||
if !pixel.hasBeenFiredEver(uniquePixelStorage: storage) { | ||
Pixel.fire(pixel: pixel, withAdditionalParameters: params, onComplete: onComplete) | ||
storage.set(Date(), forKey: pixel.name) | ||
} else { | ||
onComplete(Error.alreadyFired) | ||
} | ||
} | ||
|
||
public static func dateString(for date: Date?) -> String { | ||
guard let date else { return "" } | ||
|
||
let dateFormatter = DateFormatter() | ||
dateFormatter.calendar = Calendar.current | ||
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) | ||
dateFormatter.dateFormat = "yyyy-MM-dd" | ||
|
||
return dateFormatter.string(from: date) | ||
} | ||
} | ||
|
||
extension Pixel.Event { | ||
|
||
public func lastFireDate(uniquePixelStorage: UserDefaults) -> Date? { | ||
uniquePixelStorage.object(forKey: name) as? Date | ||
} | ||
|
||
func hasBeenFiredEver(uniquePixelStorage: UserDefaults) -> Bool { | ||
lastFireDate(uniquePixelStorage: uniquePixelStorage) != nil | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters