-
Notifications
You must be signed in to change notification settings - Fork 37
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
User segmentation migrated from iOS to BSK #1181
Merged
+4,023
−0
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9ed622f
Atb, UsageSegmentation, UsageSegmentationCalculator, UsageSegmentatio…
tomasstrba 98c967f
SwiftLint
tomasstrba 410230f
Adjusted for integration on iOS
tomasstrba 7211af3
SwiftLint
tomasstrba bdf68a7
Tests migrated from iOS repository
tomasstrba 7941d37
SwiftLint
tomasstrba 666b735
Merge branch 'main' into tom/user-segmentation
tomasstrba 884be41
new Statistics package
tomasstrba 422e770
Scheme change for the Statistics package
tomasstrba 51beaa3
Revert "Scheme change for the Statistics package"
tomasstrba 9c131d4
Revert "new Statistics package"
tomasstrba f95ee21
Merge branch 'main' into tom/user-segmentation
tomasstrba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// Atb.swift | ||
// | ||
// Copyright © 2025 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 | ||
|
||
public struct Atb: Decodable, Equatable { | ||
|
||
/// Format is v<week>-<day> | ||
/// * day is `1...7` with 1 being Wednesday | ||
/// * note that week is NOT padded but ATBs older than week 100 should never be seen by the apps, ie no one has this installed before Feb 2018 and week 99 is Jan 2018 | ||
/// * ATBs > 999 would be about 10 years in the future (Apr 2035), we can fix it nearer the time | ||
static let template = "v100-1" | ||
|
||
/// Same as `template` two characters on the end, e.g. `ma` | ||
static let templateWithVariant = template + "xx" | ||
|
||
public let version: String | ||
public let updateVersion: String? | ||
let numeric: AtbNumeric? | ||
|
||
public init(version: String, updateVersion: String?) { | ||
self.version = version | ||
self.updateVersion = updateVersion | ||
self.numeric = AtbNumeric.makeFromVersion(version) | ||
} | ||
|
||
enum CodingKeys: CodingKey { | ||
case version | ||
case updateVersion | ||
} | ||
|
||
public init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
self.version = try container.decode(String.self, forKey: .version) | ||
self.updateVersion = try container.decodeIfPresent(String.self, forKey: .updateVersion) | ||
self.numeric = AtbNumeric.makeFromVersion(version) | ||
} | ||
|
||
/// Equality is about the version without any variants. e.g. v100-1 == v100-1ma. `updateVersion` is ignored because that's a signal from the server to update the locally stored Atb so not relevant to any calculation | ||
public static func == (lhs: Atb, rhs: Atb) -> Bool { | ||
return lhs.droppingVariant == rhs.droppingVariant | ||
} | ||
|
||
/// Subtracts one ATB from the other. | ||
/// @return difference in days | ||
public static func - (lhs: Atb, rhs: Atb) -> Int { | ||
return lhs.ageInDays - rhs.ageInDays | ||
} | ||
|
||
/// Gives age in days since first ATB. If badly formatted returns -1. Only the server should be giving us ATB values, so if it is giving us something wrong there are bigger problems in the world. | ||
var ageInDays: Int { | ||
numeric?.ageInDays ?? -1 | ||
} | ||
|
||
/// Gives the current week or -1 if badly formatted | ||
var week: Int { | ||
numeric?.week ?? -1 | ||
} | ||
|
||
var isReturningUser: Bool { | ||
version.count == Self.templateWithVariant.count && version.hasSuffix("ru") | ||
} | ||
|
||
struct AtbNumeric { | ||
|
||
let week: Int | ||
let day: Int | ||
let ageInDays: Int | ||
|
||
static func makeFromVersion(_ version: String) -> AtbNumeric? { | ||
let version = String(version.prefix(Atb.template.count)) | ||
guard version.count == Atb.template.count, | ||
let week = Int(version.substring(1...3)), | ||
let day = Int(version.substring(5...5)), | ||
(1...7).contains(day) else { | ||
|
||
return nil | ||
} | ||
|
||
return AtbNumeric(week: week, day: day, ageInDays: (week * 7) + (day - 1)) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
extension Atb { | ||
|
||
var droppingVariant: String { | ||
return String(version.prefix(Atb.template.count)) | ||
} | ||
|
||
} | ||
|
||
private extension String { | ||
|
||
func substring(_ range: ClosedRange<Int>) -> String { | ||
let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound) | ||
let endIndex = self.index(self.startIndex, offsetBy: min(self.count, range.upperBound + 1)) | ||
let substring = self[startIndex..<endIndex] | ||
return String(substring) | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
Sources/BrowserServicesKit/Statistics/UsageSegmentation.swift
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,108 @@ | ||
// | ||
// UsageSegmentation.swift | ||
// | ||
// Copyright © 2025 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 | ||
import Common | ||
|
||
public enum UsageActivityType: String { | ||
|
||
case search | ||
case appUse = "app_use" | ||
|
||
} | ||
|
||
public protocol UsageSegmenting { | ||
|
||
func processATB(_ atb: Atb, withInstallAtb installAtb: Atb, andActivityType activityType: UsageActivityType) | ||
|
||
} | ||
|
||
public enum UsageSegmentationPixel { | ||
case usageSegments | ||
} | ||
|
||
public final class UsageSegmentation: UsageSegmenting { | ||
|
||
public var pixelEvents: EventMapping<UsageSegmentationPixel>? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PixelFiring replaced with EventMapping to fire pixels in clients as discussed in the tech design |
||
private let storage: UsageSegmentationStoring | ||
private let calculatorFactory: UsageSegmentationCalculatorMaking | ||
|
||
public init(pixelEvents: EventMapping<UsageSegmentationPixel>?, | ||
storage: UsageSegmentationStoring = UsageSegmentationStorage(), | ||
calculatorFactory: UsageSegmentationCalculatorMaking = DefaultCalculatorFactory()) { | ||
self.pixelEvents = pixelEvents | ||
self.storage = storage | ||
self.calculatorFactory = calculatorFactory | ||
} | ||
|
||
public func processATB(_ atb: Atb, withInstallAtb installAtb: Atb, andActivityType activityType: UsageActivityType) { | ||
var atbs = activityType.atbsFromStorage(storage) | ||
|
||
guard !atbs.contains(where: { $0 == atb }) else { return } | ||
|
||
defer { | ||
activityType.updateStorage(storage, withAtbs: atbs) | ||
} | ||
|
||
if atbs.isEmpty { | ||
atbs.append(installAtb) | ||
} | ||
|
||
if installAtb != atb { | ||
atbs.append(atb) | ||
} | ||
|
||
var pixelInfo: [String: String]? | ||
let calculator = calculatorFactory.make(installAtb: installAtb) | ||
|
||
// The calculator updates its internal state starting from the first atb, so iterate over them all and take | ||
// the last result. | ||
// | ||
// This is pretty fast (see performance test) and consider that we'll have max 1 atb per day so over a few years it's up | ||
// to the mid thousands so hardly taxing. | ||
for atb in atbs { | ||
pixelInfo = calculator.processAtb(atb, forActivityType: activityType) | ||
} | ||
|
||
if let pixelInfo { | ||
pixelEvents?.fire(.usageSegments, parameters: pixelInfo) | ||
} | ||
} | ||
|
||
} | ||
|
||
private extension UsageActivityType { | ||
|
||
func atbsFromStorage(_ storage: UsageSegmentationStoring) -> [Atb] { | ||
switch self { | ||
case .appUse: return storage.appUseAtbs | ||
case .search: return storage.searchAtbs | ||
} | ||
} | ||
|
||
func updateStorage(_ storage: UsageSegmentationStoring, withAtbs atbs: [Atb]) { | ||
var storage = storage | ||
switch self { | ||
case .appUse: | ||
storage.appUseAtbs = atbs | ||
case .search: | ||
storage.searchAtbs = atbs | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No change in the Atb.swift