-
Notifications
You must be signed in to change notification settings - Fork 423
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
usage segmentation #3263
Merged
Merged
usage segmentation #3263
Changes from 36 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
004a014
skeletons for user segment code
brindy 90797c2
Merge branch 'main' into brindy/user-segmentation
brindy f868b53
add pixel and test integration with storage and pixel firing
brindy ee058c3
rename to retention segmentation and add start of statistics loader i…
brindy 3b233af
integration with statistics loader
brindy 2111ce7
rename again
brindy 55da7c2
rename some stuff again. cope with refresh being called without an in…
brindy 6193e33
storage and test
brindy 19e80d3
correctly handle install atb
brindy c396be3
put in place test case runner
brindy 9cc16f3
Merge branch 'main' into brindy/retention-segmentation
brindy 2cf1896
separate the calculator from the segmentation class
brindy 858bd4f
create skeleton calculator based on python code
brindy d49825b
implement update state function, add atb functionality and related tests
brindy 85a9578
tidy up
brindy 340a226
get_segments first draft
brindy eb9e8e5
count as mau and active previous week
brindy 85f9ccb
count_as_mau implementation
brindy 7396312
counts as wau and active last week
brindy 8688090
implement segment regular
brindy bd86ac4
segment intermittent implementation
brindy 40d4e97
fix some horrendous spelling
brindy 3b189a1
fix get segments
brindy 8e89b3c
fix intermittent segment
brindy da5722e
Merge branch 'main' into brindy/retention-segmentation
brindy 16fa2e3
update with python changes applied and new test cases
brindy 68f662d
Optimise ATB by creating a numeric version internally
brindy 13a360b
tidy up, add a few more tests
brindy 3d45a38
switch to keyvalue storing
brindy 5b04965
check storage works with real user defaults
brindy cfaa8cd
merge from main
brindy 600b9eb
rename file to match code
brindy c530854
separate atb storage for search and app use
brindy 88e7803
fix pixel name
brindy 3337c23
no longer use a daily pixel
brindy c775df6
safer substring
brindy 879deea
remove duplication
brindy 25559c5
make classes final
brindy 08db3d3
make more classes final
brindy c492bce
Don't be fancy with the atb list
brindy dcac4bf
clarify code with a comment
brindy c036266
private func for first post install activity condition
brindy 5c5ffb8
make calc vars private
brindy 910651a
suppress warning about code not being run
brindy ad4bda3
Merge branch 'main' into brindy/retention-segmentation
brindy 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 |
---|---|---|
|
@@ -19,9 +19,105 @@ | |
|
||
import Foundation | ||
|
||
public struct Atb: Decodable { | ||
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" | ||
|
||
let version: String | ||
let updateVersion: String? | ||
let numeric: AtbNumeric? | ||
|
||
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 { | ||
|
||
if !ProcessInfo().arguments.contains("testing") { | ||
assertionFailure("bad atb") | ||
} | ||
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)) | ||
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. 👍🏼 |
||
let substring = self[startIndex..<endIndex] | ||
return String(substring) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// UsageSegmentation.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 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 | ||
|
||
enum UsageActivityType: String { | ||
|
||
case search | ||
case appUse = "app_use" | ||
|
||
} | ||
|
||
protocol UsageSegmenting { | ||
|
||
func processATB(_ atb: Atb, withInstallAtb installAtb: Atb, andActivityType activityType: UsageActivityType) | ||
|
||
} | ||
|
||
class UsageSegmentation: UsageSegmenting { | ||
aataraxiaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private let pixelFiring: PixelFiring.Type | ||
private let storage: UsageSegmentationStoring | ||
private let calculatorFactory: UsageSegmentationCalculatorMaking | ||
|
||
init(pixelFiring: PixelFiring.Type = Pixel.self, | ||
storage: UsageSegmentationStoring = UsageSegmentationStorage(), | ||
calculatorFactory: UsageSegmentationCalculatorMaking = DefaultCalculatorFactory()) { | ||
self.pixelFiring = pixelFiring | ||
self.storage = storage | ||
self.calculatorFactory = calculatorFactory | ||
} | ||
|
||
func processATB(_ atb: Atb, withInstallAtb installAtb: Atb, andActivityType activityType: UsageActivityType) { | ||
var atbs = activityType.atbsFromStorage(storage) | ||
|
||
// Fail fast by looking at the end of the list | ||
guard !atbs.reversed().contains(where: { $0 == atb }) else { return } | ||
aataraxiaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
|
||
for atb in atbs { | ||
pixelInfo = calculator.processAtb(atb, forActivityType: activityType) | ||
aataraxiaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if let pixelInfo { | ||
pixelFiring.fire(.usageSegments, withAdditionalParameters: 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.
Should we account for out-of-bounds here with something like:
and then return an Optional String.
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.
Probably using min(self.count, range.upperBound) is better and more like you'd expect from a substring function. Annoying swift just doesn't provide these tbh