-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor FXIOS-7883 [v122] Move contextual id away from sponsored til…
…e telemetry (#17610)
- Loading branch information
Showing
6 changed files
with
99 additions
and
57 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
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,33 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
import Glean | ||
|
||
// Contextual identifier used for the sponsored tiles in top sites and the suggestions in the search view | ||
struct TelemetryContextualIdentifier { | ||
enum UserDefaultsKey: String { | ||
case keyContextId = "com.moz.contextId.key" | ||
} | ||
|
||
static var contextId: String? { | ||
get { UserDefaults.standard.object(forKey: UserDefaultsKey.keyContextId.rawValue) as? String } | ||
set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKey.keyContextId.rawValue) } | ||
} | ||
|
||
static func clearUserDefaults() { | ||
TelemetryContextualIdentifier.contextId = nil | ||
} | ||
|
||
static func setupContextId() { | ||
// Use existing client UUID, if doesn't exists create a new one | ||
if let stringContextId = contextId, let clientUUID = UUID(uuidString: stringContextId) { | ||
GleanMetrics.TopSites.contextId.set(clientUUID) | ||
} else { | ||
let newUUID = UUID() | ||
GleanMetrics.TopSites.contextId.set(newUUID) | ||
contextId = newUUID.uuidString | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
firefox-ios/firefox-ios-tests/Tests/ClientTests/TelemetryContextualIdentifierTests.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,49 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
@testable import Client | ||
|
||
import Glean | ||
import XCTest | ||
|
||
class TelemetryContextualIdentifierTests: XCTestCase { | ||
override func setUp() { | ||
super.setUp() | ||
clearTest() | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
clearTest() | ||
} | ||
|
||
// MARK: Context id | ||
|
||
func testContextId_isNilByDefault() { | ||
XCTAssertNil(TelemetryContextualIdentifier.contextId) | ||
} | ||
|
||
func testContextId_isCreated() { | ||
TelemetryContextualIdentifier.setupContextId() | ||
XCTAssertNotNil(TelemetryContextualIdentifier.contextId) | ||
} | ||
|
||
func testContextId_isReusedAfterCreation() { | ||
TelemetryContextualIdentifier.setupContextId() | ||
let contextId = TelemetryContextualIdentifier.contextId | ||
TelemetryContextualIdentifier.setupContextId() | ||
XCTAssertEqual(contextId, TelemetryContextualIdentifier.contextId) | ||
} | ||
|
||
func testTelemetryWrapper_setsContextId() { | ||
TelemetryWrapper.shared.setup(profile: MockProfile()) | ||
XCTAssertNotNil(TelemetryContextualIdentifier.contextId) | ||
} | ||
|
||
// MARK: Helper methods | ||
func clearTest() { | ||
Glean.shared.resetGlean(clearStores: true) | ||
TelemetryContextualIdentifier.clearUserDefaults() | ||
} | ||
} |