-
Notifications
You must be signed in to change notification settings - Fork 261
Share tracker stats #859
Share tracker stats #859
Changes from 6 commits
cc4cf40
99f73ca
3f79b19
62d4a50
cc66fc8
19002ab
9a095ae
4445b36
e89d179
e46a87a
04e81fe
9da05db
78cced5
0f73554
c549f8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,9 @@ class BrowserViewController: UIViewController { | |
|
||
private var shouldEnsureBrowsingMode = false | ||
private var initialUrl: URL? | ||
|
||
private let userDefaultsTrackersBlockedKey = "lifetimeTrackersBlocked" | ||
private let userDefaultsShareTrackerStatsKey = "shareTrackerStats" | ||
|
||
convenience init() { | ||
self.init(nibName: nil, bundle: nil) | ||
|
@@ -244,6 +247,13 @@ class BrowserViewController: UIViewController { | |
homeView.removeFromSuperview() | ||
} | ||
self.homeView = homeView | ||
|
||
if shouldShowTrackerStatsShareButton() { | ||
let numberOfTrackersBlocked = getNumberOfLifetimeTrackersBlocked() | ||
homeView.showTrackerStatsShareButton(text: String(format: UIConstants.strings.shareTrackerStatsLabel, String(numberOfTrackersBlocked))) | ||
} else { | ||
homeView.hideTrackerStatsShareButton() | ||
} | ||
} | ||
|
||
private func createURLBar() { | ||
|
@@ -470,6 +480,40 @@ class BrowserViewController: UIViewController { | |
UIKeyCommand(input: "]", modifierFlags: .command, action: #selector(BrowserViewController.goForward), discoverabilityTitle: UIConstants.strings.browserForward), | ||
] | ||
} | ||
|
||
private func shouldShowTrackerStatsShareButton() -> Bool { | ||
let shouldShowTrackerStatsToUser = UserDefaults.standard.object(forKey: userDefaultsShareTrackerStatsKey) as! Bool? | ||
|
||
if shouldShowTrackerStatsToUser == nil { | ||
// User has not been put into a bucket for determining if it should be shown | ||
// 10% chance they get put into the group that sees the share button | ||
// arc4random_uniform(10) returns an integer 0 through 9 (inclusive) | ||
if arc4random_uniform(10) == 0 { | ||
UserDefaults.standard.set(true, forKey: userDefaultsShareTrackerStatsKey) | ||
Telemetry.default.recordEvent(category: TelemetryEventCategory.action, method: TelemetryEventMethod.shareStatsCoinFlip, object: TelemetryEventObject.showStatsShareButton) | ||
|
||
} else { | ||
UserDefaults.standard.set(false, forKey: userDefaultsShareTrackerStatsKey) | ||
return false | ||
} | ||
} | ||
|
||
if shouldShowTrackerStatsToUser == false || | ||
getNumberOfLifetimeTrackersBlocked() < 100 || | ||
NSLocale.current.identifier != "en_US" { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
private func getNumberOfLifetimeTrackersBlocked() -> Int { | ||
return UserDefaults.standard.integer(forKey: userDefaultsTrackersBlockedKey) | ||
} | ||
|
||
private func setNumberOfLifetimeTrackersBlocked(numberOfTrackers: Int) { | ||
UserDefaults.standard.set(numberOfTrackers, forKey: userDefaultsTrackersBlockedKey) | ||
} | ||
} | ||
|
||
extension BrowserViewController: URLBarDelegate { | ||
|
@@ -588,6 +632,16 @@ extension BrowserViewController: HomeViewDelegate { | |
func homeViewDidPressSettings(homeView: HomeView) { | ||
showSettings() | ||
} | ||
|
||
func shareTrackerStatsButtonTapped() { | ||
Telemetry.default.recordEvent(category: TelemetryEventCategory.action, method: TelemetryEventMethod.share, object: TelemetryEventObject.trackerStatsShareButton) | ||
|
||
let numberOfTrackersBlocked = getNumberOfLifetimeTrackersBlocked() | ||
let appStoreUrl = String(format: "https://itunes.apple.com/app/id%@", AppInfo.isKlar ? "1073435754" : "1055677337") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There has got to be a better way to do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will get you adjust links to track this install channel. Stay tuned. |
||
let text = String(format: UIConstants.strings.shareTrackerStatsText, AppInfo.productName, String(numberOfTrackersBlocked), appStoreUrl) | ||
let shareController = UIActivityViewController(activityItems: [text], applicationActivities: nil) | ||
present(shareController, animated: true) | ||
} | ||
} | ||
|
||
extension BrowserViewController: OverlayViewDelegate { | ||
|
@@ -747,6 +801,13 @@ extension BrowserViewController: WebControllerDelegate { | |
func webController(_ controller: WebController, stateDidChange state: BrowserState) {} | ||
|
||
func webController(_ controller: WebController, didUpdateTrackingProtectionStatus trackingStatus: TrackingProtectionStatus) { | ||
// Calculate the number of trackers blocked and add that to lifetime total | ||
if case .on(let info) = trackingStatus, | ||
case .on(let oldInfo) = trackingProtectionStatus { | ||
let differenceSinceLastUpdate = max(0, info.total - oldInfo.total) | ||
let numberOfTrackersBlocked = getNumberOfLifetimeTrackersBlocked() | ||
setNumberOfLifetimeTrackersBlocked(numberOfTrackers: numberOfTrackersBlocked + differenceSinceLastUpdate) | ||
} | ||
trackingProtectionStatus = trackingStatus | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ class TelemetryEventMethod { | |
public static let share = "share" | ||
public static let customDomainRemoved = "removed" | ||
public static let customDomainReordered = "reordered" | ||
public static let shareStatsCoinFlip = "share_stats_coin_flip" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be removed now, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch. will fix these |
||
} | ||
|
||
class TelemetryEventObject { | ||
|
@@ -48,4 +49,6 @@ class TelemetryEventObject { | |
public static let trackingProtectionToggle = "tracking_protection_toggle" | ||
public static let websiteLink = "website_link" | ||
public static let autofill = "autofill" | ||
public static let showStatsShareButton = "show_share_share_button" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this too? |
||
public static let trackerStatsShareButton = "tracker_stats_share_button" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is run after a user taps "Erase" so there is a chance they would not see it on launch but then see it after they erased a browsing session. Is that ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A chance they wouldn't see it on launch? And then they would see it every time they 'Erase'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh i miss understood! I thought the 10% rule was just to not show it all the time and annoy users. I will change it to bucket users into groups.