-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Web UI loading state pixels (#2531)
- Loading branch information
1 parent
ebb9320
commit 7124c49
Showing
5 changed files
with
297 additions
and
2 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
69 changes: 69 additions & 0 deletions
69
...rokerProtection/Sources/DataBrokerProtection/Pixels/DataBrokerProtectionWebUIPixels.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,69 @@ | ||
// | ||
// DataBrokerProtectionWebUIPixels.swift | ||
// | ||
// Copyright © 2023 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 | ||
import BrowserServicesKit | ||
import PixelKit | ||
|
||
final class DataBrokerProtectionWebUIPixels { | ||
|
||
enum PixelType { | ||
case loading | ||
case success | ||
} | ||
|
||
let pixelHandler: EventMapping<DataBrokerProtectionPixels> | ||
private var wasHTTPErrorPixelFired = false | ||
|
||
init(pixelHandler: EventMapping<DataBrokerProtectionPixels>) { | ||
self.pixelHandler = pixelHandler | ||
} | ||
|
||
func firePixel(for error: Error) { | ||
if wasHTTPErrorPixelFired { | ||
wasHTTPErrorPixelFired = false // We reset the flag | ||
return | ||
} | ||
|
||
let nsError = error as NSError | ||
|
||
if nsError.domain == NSURLErrorDomain { | ||
let statusCode = nsError.code | ||
if statusCode >= 400 && statusCode < 600 { | ||
pixelHandler.fire(.webUILoadingFailed(errorCategory: "httpError-\(statusCode)")) | ||
wasHTTPErrorPixelFired = true | ||
} else { | ||
pixelHandler.fire(.webUILoadingFailed(errorCategory: "other-\(nsError.code)")) | ||
} | ||
} else { | ||
pixelHandler.fire(.webUILoadingFailed(errorCategory: "other-\(nsError.code)")) | ||
} | ||
} | ||
|
||
func firePixel(for selectedURL: DataBrokerProtectionWebUIURLType, type: PixelType) { | ||
let environment = selectedURL == .custom ? "staging" : "production" | ||
|
||
switch type { | ||
case .loading: | ||
pixelHandler.fire(.webUILoadingStarted(environment: environment)) | ||
case .success: | ||
pixelHandler.fire(.webUILoadingSuccess(environment: environment)) | ||
} | ||
} | ||
} |
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
168 changes: 168 additions & 0 deletions
168
...okerProtection/Tests/DataBrokerProtectionTests/DataBrokerProtectionWebUIPixelsTests.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,168 @@ | ||
// | ||
// DataBrokerProtectionWebUIPixelsTests.swift | ||
// | ||
// Copyright © 2023 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 XCTest | ||
import Foundation | ||
@testable import DataBrokerProtection | ||
|
||
final class DataBrokerProtectionWebUIPixelsTests: XCTestCase { | ||
|
||
let handler = MockDataBrokerProtectionPixelsHandler() | ||
|
||
override func tearDown() { | ||
handler.clear() | ||
} | ||
|
||
func testWhenURLErrorIsHttp_thenCorrectPixelIsFired() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: NSError(domain: NSURLErrorDomain, code: 404)) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.params!["error_category"], | ||
"httpError-404" | ||
) | ||
} | ||
|
||
func testWhenURLErrorIsNotHttp_thenCorrectPixelIsFired() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: NSError(domain: NSURLErrorDomain, code: 100)) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.params!["error_category"], | ||
"other-100" | ||
) | ||
} | ||
|
||
func testWhenErrorIsNotURL_thenCorrectPixelIsFired() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: NSError(domain: NSCocoaErrorDomain, code: 500)) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.params!["error_category"], | ||
"other-500" | ||
) | ||
} | ||
|
||
func testWhenSelectedURLisCustomAndLoading_thenStagingParamIsSent() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: .custom, type: .loading) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.name, | ||
DataBrokerProtectionPixels.webUILoadingStarted(environment: "staging").name | ||
) | ||
XCTAssertEqual( | ||
lastPixelFired.params!["environment"], | ||
"staging" | ||
) | ||
} | ||
|
||
func testWhenSelectedURLisProductionAndLoading_thenProductionParamIsSent() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: .production, type: .loading) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.name, | ||
DataBrokerProtectionPixels.webUILoadingStarted(environment: "staging").name | ||
) | ||
XCTAssertEqual( | ||
lastPixelFired.params!["environment"], | ||
"production" | ||
) | ||
} | ||
|
||
func testWhenSelectedURLisCustomAndSuccess_thenStagingParamIsSent() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: .custom, type: .success) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.name, | ||
DataBrokerProtectionPixels.webUILoadingSuccess(environment: "staging").name | ||
) | ||
XCTAssertEqual( | ||
lastPixelFired.params!["environment"], | ||
"staging" | ||
) | ||
} | ||
|
||
func testWhenSelectedURLisProductionAndSuccess_thenProductionParamIsSent() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: .production, type: .success) | ||
|
||
let lastPixelFired = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
lastPixelFired.name, | ||
DataBrokerProtectionPixels.webUILoadingSuccess(environment: "staging").name | ||
) | ||
XCTAssertEqual( | ||
lastPixelFired.params!["environment"], | ||
"production" | ||
) | ||
} | ||
|
||
func testWhenHTTPPixelIsFired_weDoNotFireAnotherPixelRightAway() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: NSError(domain: NSURLErrorDomain, code: 404)) | ||
sut.firePixel(for: NSError(domain: NSCocoaErrorDomain, code: 500)) | ||
|
||
let httpPixel = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
httpPixel.params!["error_category"], | ||
"httpError-404" | ||
) | ||
XCTAssertEqual(MockDataBrokerProtectionPixelsHandler.lastPixelsFired.count, 1) // We only fire one pixel | ||
} | ||
|
||
func testWhenHTTPPixelIsFired_weFireTheNextErrorPixelOnTheSecondTry() { | ||
let sut = DataBrokerProtectionWebUIPixels(pixelHandler: handler) | ||
|
||
sut.firePixel(for: NSError(domain: NSURLErrorDomain, code: 404)) | ||
sut.firePixel(for: NSError(domain: NSCocoaErrorDomain, code: 500)) | ||
sut.firePixel(for: NSError(domain: NSCocoaErrorDomain, code: 500)) | ||
|
||
let httpPixel = MockDataBrokerProtectionPixelsHandler.lastPixelsFired.first! | ||
|
||
XCTAssertEqual( | ||
httpPixel.params!["error_category"], | ||
"httpError-404" | ||
) | ||
XCTAssertEqual(MockDataBrokerProtectionPixelsHandler.lastPixelsFired.count, 2) // We fire the HTTP pixel and the second cocoa error pixel | ||
} | ||
} |