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

usage segmentation #3263

Merged
merged 45 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
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 Aug 21, 2024
90797c2
Merge branch 'main' into brindy/user-segmentation
brindy Aug 21, 2024
f868b53
add pixel and test integration with storage and pixel firing
brindy Aug 21, 2024
ee058c3
rename to retention segmentation and add start of statistics loader i…
brindy Aug 21, 2024
3b233af
integration with statistics loader
brindy Aug 21, 2024
2111ce7
rename again
brindy Aug 22, 2024
55da7c2
rename some stuff again. cope with refresh being called without an in…
brindy Aug 22, 2024
6193e33
storage and test
brindy Aug 22, 2024
19e80d3
correctly handle install atb
brindy Aug 22, 2024
c396be3
put in place test case runner
brindy Aug 23, 2024
9cc16f3
Merge branch 'main' into brindy/retention-segmentation
brindy Aug 26, 2024
2cf1896
separate the calculator from the segmentation class
brindy Aug 26, 2024
858bd4f
create skeleton calculator based on python code
brindy Aug 26, 2024
d49825b
implement update state function, add atb functionality and related tests
brindy Aug 28, 2024
85a9578
tidy up
brindy Aug 28, 2024
340a226
get_segments first draft
brindy Aug 28, 2024
eb9e8e5
count as mau and active previous week
brindy Aug 28, 2024
85f9ccb
count_as_mau implementation
brindy Aug 28, 2024
7396312
counts as wau and active last week
brindy Aug 28, 2024
8688090
implement segment regular
brindy Aug 28, 2024
bd86ac4
segment intermittent implementation
brindy Aug 28, 2024
40d4e97
fix some horrendous spelling
brindy Aug 28, 2024
3b189a1
fix get segments
brindy Aug 28, 2024
8e89b3c
fix intermittent segment
brindy Aug 28, 2024
da5722e
Merge branch 'main' into brindy/retention-segmentation
brindy Aug 28, 2024
16fa2e3
update with python changes applied and new test cases
brindy Aug 29, 2024
68f662d
Optimise ATB by creating a numeric version internally
brindy Aug 29, 2024
13a360b
tidy up, add a few more tests
brindy Aug 30, 2024
3d45a38
switch to keyvalue storing
brindy Aug 30, 2024
5b04965
check storage works with real user defaults
brindy Aug 30, 2024
cfaa8cd
merge from main
brindy Sep 2, 2024
600b9eb
rename file to match code
brindy Sep 2, 2024
c530854
separate atb storage for search and app use
brindy Sep 2, 2024
88e7803
fix pixel name
brindy Sep 2, 2024
3337c23
no longer use a daily pixel
brindy Sep 2, 2024
c775df6
safer substring
brindy Sep 3, 2024
879deea
remove duplication
brindy Sep 3, 2024
25559c5
make classes final
brindy Sep 3, 2024
08db3d3
make more classes final
brindy Sep 3, 2024
c492bce
Don't be fancy with the atb list
brindy Sep 3, 2024
dcac4bf
clarify code with a comment
brindy Sep 3, 2024
c036266
private func for first post install activity condition
brindy Sep 3, 2024
5c5ffb8
make calc vars private
brindy Sep 3, 2024
910651a
suppress warning about code not being run
brindy Sep 3, 2024
ad4bda3
Merge branch 'main' into brindy/retention-segmentation
brindy Sep 3, 2024
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
98 changes: 97 additions & 1 deletion Core/Atb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

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:

guard range.lowerBound >= 0, 
              range.upperBound < self.count else {
     return nil
}   

and then return an Optional String.

Copy link
Contributor Author

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

let endIndex = self.index(self.startIndex, offsetBy: min(self.count, range.upperBound + 1))
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏼

let substring = self[startIndex..<endIndex]
return String(substring)
}

}
6 changes: 6 additions & 0 deletions Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ extension Pixel {
case duckPlayerContingencySettingsDisplayed
case duckPlayerContingencyLearnMoreClicked

// MARK: enhanced statistics
case usageSegments

// MARK: Certificate warnings
case certificateWarningDisplayed(_ errorType: String)
case certificateWarningLeaveClicked
Expand Down Expand Up @@ -1583,6 +1586,9 @@ extension Pixel.Event {
case .duckPlayerContingencySettingsDisplayed: return "duckplayer_ios_contingency_settings-displayed"
case .duckPlayerContingencyLearnMoreClicked: return "duckplayer_ios_contingency_learn-more-clicked"

// MARK: Enhanced statistics
case .usageSegments: return "m_retention_segments"

// MARK: Certificate warnings
case .certificateWarningDisplayed(let errorType):
return "m_certificate_warning_displayed_\(errorType)"
Expand Down
29 changes: 26 additions & 3 deletions Core/StatisticsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public class StatisticsLoader {

private let statisticsStore: StatisticsStore
private let returnUserMeasurement: ReturnUserMeasurement
private let usageSegmentation: UsageSegmenting
private let parser = AtbParser()

init(statisticsStore: StatisticsStore = StatisticsUserDefaults(),
returnUserMeasurement: ReturnUserMeasurement = KeychainReturnUserMeasurement()) {
returnUserMeasurement: ReturnUserMeasurement = KeychainReturnUserMeasurement(),
usageSegmentation: UsageSegmenting = UsageSegmentation()) {
self.statisticsStore = statisticsStore
self.returnUserMeasurement = returnUserMeasurement
self.usageSegmentation = usageSegmentation
}

public func load(completion: @escaping Completion = {}) {
Expand Down Expand Up @@ -88,7 +91,10 @@ public class StatisticsLoader {

public func refreshSearchRetentionAtb(completion: @escaping Completion = {}) {
guard let url = StatisticsDependentURLFactory(statisticsStore: statisticsStore).makeSearchAtbURL() else {
requestInstallStatistics(completion: completion)
requestInstallStatistics {
self.updateUsageSegmentationAfterInstall(activityType: .search)
completion()
}
return
}

Expand All @@ -104,6 +110,7 @@ public class StatisticsLoader {
if let data = response?.data, let atb = try? self.parser.convert(fromJsonData: data) {
self.statisticsStore.searchRetentionAtb = atb.version
self.storeUpdateVersionIfPresent(atb)
self.updateUsageSegmentationWithAtb(atb, activityType: .search)
NotificationCenter.default.post(name: .searchDAU,
object: nil, userInfo: nil)
}
Expand All @@ -113,7 +120,10 @@ public class StatisticsLoader {

public func refreshAppRetentionAtb(completion: @escaping Completion = {}) {
guard let url = StatisticsDependentURLFactory(statisticsStore: statisticsStore).makeAppAtbURL() else {
requestInstallStatistics(completion: completion)
requestInstallStatistics {
self.updateUsageSegmentationAfterInstall(activityType: .appUse)
completion()
}
return
}

Expand All @@ -129,6 +139,7 @@ public class StatisticsLoader {
if let data = response?.data, let atb = try? self.parser.convert(fromJsonData: data) {
self.statisticsStore.appRetentionAtb = atb.version
self.storeUpdateVersionIfPresent(atb)
self.updateUsageSegmentationWithAtb(atb, activityType: .appUse)
}
completion()
}
Expand All @@ -141,4 +152,16 @@ public class StatisticsLoader {
returnUserMeasurement.updateStoredATB(atb)
}
}

private func updateUsageSegmentationWithAtb(_ atb: Atb, activityType: UsageActivityType) {
guard let installAtbValue = statisticsStore.atb else { return }
let installAtb = Atb(version: installAtbValue, updateVersion: nil)
self.usageSegmentation.processATB(atb, withInstallAtb: installAtb, andActivityType: activityType)
}

private func updateUsageSegmentationAfterInstall(activityType: UsageActivityType) {
guard let installAtbValue = statisticsStore.atb else { return }
let installAtb = Atb(version: installAtbValue, updateVersion: nil)
self.usageSegmentation.processATB(installAtb, withInstallAtb: installAtb, andActivityType: activityType)
}
aataraxiaa marked this conversation as resolved.
Show resolved Hide resolved
}
100 changes: 100 additions & 0 deletions Core/UsageSegmentation.swift
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
}
}

}
Loading
Loading