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

Add install pixel #3656

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import NetworkProtection
extension Pixel {

public enum Event {


case appInstall
case appLaunch
case refreshPressed
case pullToRefresh
Expand Down Expand Up @@ -908,6 +909,7 @@ extension Pixel.Event {

public var name: String {
switch self {
case .appInstall: return "m_install"
case .appLaunch: return "ml"
case .refreshPressed: return "m_r"
case .pullToRefresh: return "m_pull-to-reload"
Expand Down
20 changes: 19 additions & 1 deletion Core/StatisticsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@
private let parser = AtbParser()
private let atbPresenceFileMarker = BoolFileMarker(name: .isATBPresent)
private let inconsistencyMonitoring: StatisticsStoreInconsistencyMonitoring
private let pixelFiring: PixelFiring.Type

init(statisticsStore: StatisticsStore = StatisticsUserDefaults(),
returnUserMeasurement: ReturnUserMeasurement = KeychainReturnUserMeasurement(),
usageSegmentation: UsageSegmenting = UsageSegmentation(),
inconsistencyMonitoring: StatisticsStoreInconsistencyMonitoring = StorageInconsistencyMonitor()) {
inconsistencyMonitoring: StatisticsStoreInconsistencyMonitoring = StorageInconsistencyMonitor(),
pixelFiring: PixelFiring.Type = Pixel.self) {
self.statisticsStore = statisticsStore
self.returnUserMeasurement = returnUserMeasurement
self.usageSegmentation = usageSegmentation
self.inconsistencyMonitoring = inconsistencyMonitoring
self.pixelFiring = pixelFiring
}

public func load(completion: @escaping Completion = {}) {
Expand All @@ -66,7 +69,7 @@
let configuration = APIRequest.Configuration(url: .atb)
let request = APIRequest(configuration: configuration, urlSession: .session())

request.fetch { response, error in

Check warning on line 72 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 72 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 72 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

'fetch(completion:)' is deprecated: Please use 'APIService' instead.
if let error = error {
Logger.general.error("Initial atb request failed with error: \(error.localizedDescription, privacy: .public)")
completion()
Expand All @@ -88,12 +91,13 @@
let configuration = APIRequest.Configuration(url: url)
let request = APIRequest(configuration: configuration, urlSession: .session())

request.fetch { _, error in

Check warning on line 94 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 94 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

'fetch(completion:)' is deprecated: Please use 'APIService' instead.
if let error = error {
Logger.general.error("Exit request failed with error: \(error.localizedDescription, privacy: .public)")
completion()
return
}
self.fireInstallPixel()
self.statisticsStore.installDate = Date()
self.statisticsStore.atb = atb.version
self.returnUserMeasurement.installCompletedWithATB(atb)
Expand All @@ -102,6 +106,20 @@
}
}

private func fireInstallPixel() {
let formattedLocale = Locale.current.localeIdentifierAsJsonFormat
let isReinstall = String(statisticsStore.variant == VariantIOS.returningUser.name)
let parameters = [
"locale": formattedLocale,
"reinstall": isReinstall
]
pixelFiring.fire(.appInstall, withAdditionalParameters: parameters, includedParameters: [.appVersion], onComplete: { error in
if let error {
Logger.general.error("Install pixel failed with error: \(error.localizedDescription, privacy: .public)")
}
})
}

private func createATBFileMarker() {
atbPresenceFileMarker?.mark()
}
Expand All @@ -118,7 +136,7 @@
let configuration = APIRequest.Configuration(url: url)
let request = APIRequest(configuration: configuration, urlSession: .session())

request.fetch { response, error in

Check warning on line 139 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 139 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

'fetch(completion:)' is deprecated: Please use 'APIService' instead.
if let error = error {
Logger.general.error("Search atb request failed with error: \(error.localizedDescription, privacy: .public)")
completion()
Expand Down Expand Up @@ -147,7 +165,7 @@
let configuration = APIRequest.Configuration(url: url)
let request = APIRequest(configuration: configuration, urlSession: .session())

request.fetch { response, error in

Check warning on line 168 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 168 in Core/StatisticsLoader.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

'fetch(completion:)' is deprecated: Please use 'APIService' instead.
if let error = error {
Logger.general.error("App atb request failed with error: \(error.localizedDescription, privacy: .public)")
completion()
Expand Down
21 changes: 20 additions & 1 deletion DuckDuckGoTests/StatisticsLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,26 @@ class StatisticsLoaderTests: XCTestCase {

var mockStatisticsStore: StatisticsStore!
var mockUsageSegmentation: MockUsageSegmentation!
var mockPixelFiring: PixelFiringMock.Type!
var testee: StatisticsLoader!

override func setUpWithError() throws {
try super.setUpWithError()

PixelFiringMock.tearDown()

mockPixelFiring = PixelFiringMock.self
mockStatisticsStore = MockStatisticsStore()
mockUsageSegmentation = MockUsageSegmentation()
testee = StatisticsLoader(statisticsStore: mockStatisticsStore,
usageSegmentation: mockUsageSegmentation,
inconsistencyMonitoring: MockStatisticsStoreInconsistencyMonitoring())
inconsistencyMonitoring: MockStatisticsStoreInconsistencyMonitoring(),
pixelFiring: mockPixelFiring)
}

override func tearDown() {
HTTPStubs.removeAllStubs()
PixelFiringMock.tearDown()
super.tearDown()
}

Expand Down Expand Up @@ -270,6 +276,19 @@ class StatisticsLoaderTests: XCTestCase {
waitForExpectations(timeout: 5, handler: nil)
}

func testWhenInstallStatisticsRequestedThenInstallPixelIsFired() {
loadSuccessfulExiStub()

let testExpectation = expectation(description: "refresh complete")
testee.refreshAppRetentionAtb {
Thread.sleep(forTimeInterval: .seconds(0.1))
testExpectation.fulfill()
Comment on lines +284 to +285
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This small delay isn't ideal; it's in place since the refreshAppRetentionAtb callback is waiting for the exti call, not the pixel call, so there is a chance that this could complete before the pixel call has had time to do its thing.

It would be better to wait for the pixel firing mock to have the right value, with a ~1 second timeout. I'll look at updating this.

}

wait(for: [testExpectation], timeout: 5.0)
XCTAssertEqual(mockPixelFiring.lastPixelName, Pixel.Event.appInstall.name)
}

func loadSuccessfulAtbStub() {
stub(condition: isHost(URL.atb.host!)) { _ in
let path = OHPathForFile("MockFiles/atb.json", type(of: self))!
Expand Down
Loading