Skip to content

Commit

Permalink
Add Privacy Pro survey parameters (#2920)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/414235014887631/1207500645737162/f
Tech Design URL:
CC:

Description:

This PR adds new survey parameters for Privacy Pro.
  • Loading branch information
samsymons authored Jun 9, 2024
1 parent 18812c9 commit ce613dd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9770,7 +9770,7 @@
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 152.0.1;
version = 153.0.0;
};
};
9F8FE9472BAE50E50071E372 /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "43d6c090699ddc1b92c0c016dc31b923fb06f59f",
"version" : "152.0.1"
"revision" : "b78ae617c7fe66244741f489158a1f40e567e674",
"version" : "153.0.0"
}
},
{
Expand Down
8 changes: 7 additions & 1 deletion DuckDuckGo/RemoteMessaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ struct RemoteMessaging {
isWidgetInstalled: isWidgetInstalled)
}

// swiftlint:disable:next function_body_length
static private func fetchAndProcess(bookmarksCount: Int,
favoritesCount: Int,
remoteMessagingStore: RemoteMessagingStore = AppDependencyProvider.shared.remoteMessagingStore,
Expand All @@ -159,10 +160,10 @@ struct RemoteMessaging {

let activationDateStore = DefaultVPNActivationDateStore()
let daysSinceNetworkProtectionEnabled = activationDateStore.daysSinceActivation() ?? -1
let surveyActionMapper = DefaultRemoteMessagingSurveyURLBuilder(statisticsStore: statisticsStore)

var privacyProDaysSinceSubscribed: Int = -1
var privacyProDaysUntilExpiry: Int = -1
let surveyActionMapper: DefaultRemoteMessagingSurveyURLBuilder

if let accessToken = AppDependencyProvider.shared.subscriptionManager.accountManager.accessToken {
let subscriptionResult = await AppDependencyProvider.shared.subscriptionManager.subscriptionService.getSubscription(
Expand All @@ -172,7 +173,12 @@ struct RemoteMessaging {
if case let .success(subscription) = subscriptionResult {
privacyProDaysSinceSubscribed = Calendar.current.numberOfDaysBetween(subscription.startedAt, and: Date()) ?? -1
privacyProDaysUntilExpiry = Calendar.current.numberOfDaysBetween(Date(), and: subscription.expiresOrRenewsAt) ?? -1
surveyActionMapper = DefaultRemoteMessagingSurveyURLBuilder(statisticsStore: statisticsStore, subscription: subscription)
} else {
surveyActionMapper = DefaultRemoteMessagingSurveyURLBuilder(statisticsStore: statisticsStore, subscription: nil)
}
} else {
surveyActionMapper = DefaultRemoteMessagingSurveyURLBuilder(statisticsStore: statisticsStore, subscription: nil)
}

let remoteMessagingConfigMatcher = RemoteMessagingConfigMatcher(
Expand Down
63 changes: 54 additions & 9 deletions DuckDuckGo/RemoteMessagingSurveyURLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ import BrowserServicesKit
import RemoteMessaging
import Core
import Common
import Subscription

struct DefaultRemoteMessagingSurveyURLBuilder: RemoteMessagingSurveyActionMapping {

private let statisticsStore: StatisticsStore
private let activationDateStore: VPNActivationDateStore
private let vpnActivationDateStore: VPNActivationDateStore
private let subscription: Subscription?

init(statisticsStore: StatisticsStore = StatisticsUserDefaults(),
activationDateStore: VPNActivationDateStore = DefaultVPNActivationDateStore()) {
vpnActivationDateStore: VPNActivationDateStore = DefaultVPNActivationDateStore(),
subscription: Subscription?) {
self.statisticsStore = statisticsStore
self.activationDateStore = activationDateStore
self.vpnActivationDateStore = vpnActivationDateStore
self.subscription = subscription
}

// swiftlint:disable:next cyclomatic_complexity
// swiftlint:disable:next cyclomatic_complexity function_body_length
func add(parameters: [RemoteMessagingSurveyActionParameter], to surveyURL: URL) -> URL {
guard var components = URLComponents(string: surveyURL.absoluteString) else {
assertionFailure("Could not build URL components from survey URL")
Expand All @@ -60,15 +64,56 @@ struct DefaultRemoteMessagingSurveyURLBuilder: RemoteMessagingSurveyActionMappin
case .hardwareModel:
let model = hardwareModel().addingPercentEncoding(withAllowedCharacters: .alphanumerics)
queryItems.append(URLQueryItem(name: parameter.rawValue, value: model))
case .lastActiveDate:
if let daysSinceLastActive = activationDateStore.daysSinceLastActive() {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: daysSinceLastActive)))
}
case .daysInstalled:
if let installDate = statisticsStore.installDate,
let daysSinceInstall = Calendar.current.numberOfDaysBetween(installDate, and: Date()) {
let daysSinceInstall = Calendar.current.numberOfDaysBetween(installDate, and: Date()) {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: daysSinceInstall)))
}
case .privacyProStatus:
switch subscription?.status {
case .autoRenewable: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "auto_renewable"))
case .notAutoRenewable: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "not_auto_renewable"))
case .gracePeriod: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "grace_period"))
case .inactive: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "inactive"))
case .expired: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "expired"))
case .unknown: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "unknown"))
case nil: break
}
case .privacyProPlatform:

switch subscription?.platform {
case .apple: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "apple"))
case .google: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "google"))
case .stripe: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "stripe"))
case .unknown: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "unknown"))
case nil: break
}
case .privacyProBilling:
switch subscription?.billingPeriod {
case .monthly: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "monthly"))
case .yearly: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "yearly"))
case .unknown: queryItems.append(URLQueryItem(name: parameter.rawValue, value: "unknown"))
case nil: break
}

case .privacyProDaysSincePurchase:
if let startDate = subscription?.startedAt,
let daysSincePurchase = Calendar.current.numberOfDaysBetween(startDate, and: Date()) {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: daysSincePurchase)))
}
case .privacyProDaysUntilExpiry:
if let expiryDate = subscription?.expiresOrRenewsAt,
let daysUntilExpiry = Calendar.current.numberOfDaysBetween(Date(), and: expiryDate) {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: daysUntilExpiry)))
}
case .vpnFirstUsed:
if let vpnFirstUsed = vpnActivationDateStore.daysSinceActivation() {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: vpnFirstUsed)))
}
case .vpnLastUsed:
if let vpnLastUsed = vpnActivationDateStore.daysSinceLastActive() {
queryItems.append(URLQueryItem(name: parameter.rawValue, value: String(describing: vpnLastUsed)))
}
}
}

Expand Down

0 comments on commit ce613dd

Please sign in to comment.