-
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.
Cherry pick Subscription Attribution changes (#2790)
Task/Issue URL: https://app.asana.com/0/1199230911884351/1205406454505225/f **Description**: Cherry pick the changes from this [PR](#2761)
- Loading branch information
1 parent
5864a4a
commit 0a060e9
Showing
20 changed files
with
551 additions
and
109 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
97 changes: 97 additions & 0 deletions
97
DuckDuckGo/Common/Attribution/AttributionPixelHandler.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,97 @@ | ||
// | ||
// AttributionPixelHandler.swift | ||
// | ||
// 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 | ||
import PixelKit | ||
|
||
// A type that send pixels that needs attributions parameters. | ||
protocol AttributionPixelHandler { | ||
func fireAttributionPixel( | ||
event: PixelKit.Event, | ||
frequency: PixelKit.Frequency, | ||
origin: String?, | ||
additionalParameters: [String: String]? | ||
) | ||
} | ||
|
||
final class GenericAttributionPixelHandler: AttributionPixelHandler { | ||
enum Parameters { | ||
static let origin = "origin" | ||
static let locale = "locale" | ||
} | ||
|
||
private let fireRequest: FireRequest | ||
private let locale: Locale | ||
|
||
/// Creates an instance with the specified fire request, origin provider and locale. | ||
/// - Parameters: | ||
/// - fireRequest: A function for sending the Pixel request. | ||
/// - locale: The locale of the device. | ||
init( | ||
fireRequest: @escaping FireRequest = PixelKit.fire, | ||
locale: Locale = .current | ||
) { | ||
self.fireRequest = fireRequest | ||
self.locale = locale | ||
} | ||
|
||
func fireAttributionPixel( | ||
event: PixelKit.Event, | ||
frequency: PixelKit.Frequency, | ||
origin: String?, | ||
additionalParameters: [String: String]? | ||
) { | ||
fireRequest( | ||
event, | ||
frequency, | ||
[:], | ||
self.parameters(additionalParameters, withOrigin: origin, locale: locale.identifier), | ||
nil, | ||
nil, | ||
true, { _, _ in } | ||
) | ||
} | ||
} | ||
|
||
// MARK: - Parameter | ||
|
||
private extension GenericAttributionPixelHandler { | ||
func parameters(_ parameters: [String: String]?, withOrigin origin: String?, locale: String) -> [String: String] { | ||
var parameters = parameters ?? [:] | ||
parameters[Self.Parameters.locale] = locale | ||
if let origin { | ||
parameters[Self.Parameters.origin] = origin | ||
} | ||
return parameters | ||
} | ||
} | ||
|
||
// MARK: - FireRequest | ||
|
||
extension GenericAttributionPixelHandler { | ||
typealias FireRequest = ( | ||
_ event: PixelKit.Event, | ||
_ frequency: PixelKit.Frequency, | ||
_ headers: [String: String], | ||
_ parameters: [String: String]?, | ||
_ error: Error?, | ||
_ allowedQueryReservedCharacters: CharacterSet?, | ||
_ includeAppVersionParameter: Bool, | ||
_ onComplete: @escaping (Bool, Error?) -> Void | ||
) -> Void | ||
} |
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 |
---|---|---|
|
@@ -581,4 +581,5 @@ extension URL { | |
return false | ||
} | ||
} | ||
|
||
} |
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
46 changes: 46 additions & 0 deletions
46
DuckDuckGo/Subscription/SubscriptionAttributionPixelHandler.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,46 @@ | ||
// | ||
// SubscriptionAttributionPixelHandler.swift | ||
// | ||
// 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 | ||
import Subscription | ||
|
||
protocol SubscriptionAttributionPixelHandler: AnyObject { | ||
var origin: String? { get set } | ||
func fireSuccessfulSubscriptionAttributionPixel() | ||
} | ||
|
||
// MARK: - SubscriptionAttributionPixelHandler | ||
|
||
final class PrivacyProSubscriptionAttributionPixelHandler: SubscriptionAttributionPixelHandler { | ||
var origin: String? | ||
private let decoratedAttributionPixelHandler: AttributionPixelHandler | ||
|
||
init(attributionPixelHandler: AttributionPixelHandler = GenericAttributionPixelHandler()) { | ||
decoratedAttributionPixelHandler = attributionPixelHandler | ||
} | ||
|
||
func fireSuccessfulSubscriptionAttributionPixel() { | ||
decoratedAttributionPixelHandler.fireAttributionPixel( | ||
event: PrivacyProPixel.privacyProSuccessfulSubscriptionAttribution, | ||
frequency: .standard, | ||
origin: origin, | ||
additionalParameters: nil | ||
) | ||
} | ||
|
||
} |
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,66 @@ | ||
// | ||
// SubscriptionRedirectManager.swift | ||
// | ||
// 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 | ||
import Subscription | ||
import BrowserServicesKit | ||
|
||
protocol SubscriptionRedirectManager: AnyObject { | ||
func redirectURL(for url: URL) -> URL? | ||
} | ||
|
||
final class PrivacyProSubscriptionRedirectManager: SubscriptionRedirectManager { | ||
private let featureAvailabiltyProvider: () -> Bool | ||
|
||
init(featureAvailabiltyProvider: @escaping @autoclosure () -> Bool = DefaultSubscriptionFeatureAvailability().isFeatureAvailable) { | ||
self.featureAvailabiltyProvider = featureAvailabiltyProvider | ||
} | ||
|
||
func redirectURL(for url: URL) -> URL? { | ||
guard url.isPart(ofDomain: "duckduckgo.com") else { return nil } | ||
|
||
if url.pathComponents == URL.privacyPro.pathComponents { | ||
let isFeatureAvailable = featureAvailabiltyProvider() | ||
let shouldHidePrivacyProDueToNoProducts = SubscriptionPurchaseEnvironment.current == .appStore && SubscriptionPurchaseEnvironment.canPurchase == false | ||
let isPurchasePageRedirectActive = isFeatureAvailable && !shouldHidePrivacyProDueToNoProducts | ||
// Redirect the `/pro` URL to `/subscriptions` URL. If there are any query items in the original URL it appends to the `/subscriptions` URL. | ||
return isPurchasePageRedirectActive ? URL.subscriptionBaseURL.addingQueryItems(from: url) : nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
} | ||
|
||
private extension URL { | ||
|
||
func addingQueryItems(from url: URL) -> URL { | ||
// If the origin value is of type "do+something" appending the percentEncodedQueryItem crashes the browser as + is replaced by a space. | ||
// Perform encoding on the value to avoid the crash. | ||
guard let queryItems = url.getQueryItems()? | ||
.compactMap({ queryItem -> URLQueryItem? in | ||
guard let value = queryItem.value else { return nil } | ||
let encodedValue = value.percentEncoded(withAllowedCharacters: .urlQueryParameterAllowed) | ||
return URLQueryItem(name: queryItem.name, value: encodedValue) | ||
}) | ||
else { return self } | ||
|
||
return self.appending(percentEncodedQueryItems: queryItems) | ||
} | ||
|
||
} |
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
Oops, something went wrong.