Skip to content

Commit

Permalink
Add leading 0 to dau appVersion param.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Buczek committed Nov 23, 2017
1 parent cd330ab commit 151a7d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
27 changes: 22 additions & 5 deletions analytics/DAU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Shared
// TODO: Separate logger for this kind of work?
private let log = Logger.browserLogger

// Unit tests for DAU are located in brave/tests_src/unit/DauTest.swift.
struct DAU {
public static let preferencesKey = "dau_stat"
public static let weekOfInstallationKeyPrefKey = "week_of_installation"
Expand All @@ -30,14 +31,12 @@ struct DAU {
public func sendPingToServer() {
guard let params = paramsAndPrefsSetup() else {
log.debug("dau, no changes detected, no server ping")
print("bxx dau, no changes detected, no server ping")
return
}

// Sending ping to server
let fullUrl = baseUrl + params
log.debug("send ping to server, url: \(fullUrl)")
print("bxx send ping to server, url: \(fullUrl)")

guard let url = URL(string: fullUrl) else {
if !BraveUX.IsRelease {
Expand Down Expand Up @@ -95,10 +94,28 @@ struct DAU {
return "&channel=\(BraveUX.IsRelease ? "stable" : "beta")"
}

// TODO: Add leading `.0` to so app version is always in format x.x.x.
// See issue #1337 for more info.
var versionParam: String {
return "&version=\(AppInfo.appVersion)"
var version = AppInfo.appVersion

if DAU.shouldAppend0ToAppVersion(version) {
version += ".0"
}

return "&version=\(version)"
}

/// All app versions for dau pings must be saved in x.x.x format where x are digits.
static func shouldAppend0ToAppVersion(_ version: String) -> Bool {
let correctAppVersionPattern = "^\\d+.\\d+$"
do {
let regex = try NSRegularExpression(pattern: correctAppVersionPattern, options: [])
let match = regex.firstMatch(in: version, options: [], range: NSRange(location: 0, length: version.count))

return match != nil
} catch {
log.error("Version regex pattern error")
return false
}
}

func firstLaunchParam(_ isFirst: Bool) -> String {
Expand Down
25 changes: 20 additions & 5 deletions brave/tests_src/unit/DauTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DauTest: XCTestCase {
XCTAssertNotNil(prefs.stringForKey(DAU.weekOfInstallationKeyPrefKey))

XCTAssertEqual(params!,
"&channel=beta&version=\(AppInfo.appVersion)&daily=true&weekly=true&monthly=true&first=true&woi=2017-11-20")
"&channel=beta&version=\(appVersion)&daily=true&weekly=true&monthly=true&first=true&woi=2017-11-20")
}

func testNotFirstLaunchSkipDau() {
Expand Down Expand Up @@ -75,21 +75,21 @@ class DauTest: XCTestCase {
let dailyParams = dauSecondLaunch.paramsAndPrefsSetup()
XCTAssertNotNil(dailyParams)
XCTAssertEqual(dailyParams!,
"&channel=beta&version=\(AppInfo.appVersion)&daily=true&weekly=false&monthly=false&first=false&woi=\(woiPrefs)")
"&channel=beta&version=\(appVersion)&daily=true&weekly=false&monthly=false&first=false&woi=\(woiPrefs)")

// Weekly check
let weeklyFutureDate = dateFrom(string: "2017-11-30")
let weeklyParams = DAU(prefs: prefs, date: weeklyFutureDate).paramsAndPrefsSetup()
XCTAssertNotNil(weeklyParams)
XCTAssertEqual(weeklyParams!,
"&channel=beta&version=\(AppInfo.appVersion)&daily=true&weekly=true&monthly=false&first=false&woi=\(woiPrefs)")
"&channel=beta&version=\(appVersion)&daily=true&weekly=true&monthly=false&first=false&woi=\(woiPrefs)")

// Monthly check
let monthlyFutureDate = dateFrom(string: "2017-12-20")
let monthlyParams = DAU(prefs: prefs, date: monthlyFutureDate).paramsAndPrefsSetup()
XCTAssertNotNil(monthlyParams)
XCTAssertEqual(monthlyParams!,
"&channel=beta&version=\(AppInfo.appVersion)&daily=true&weekly=true&monthly=true&first=false&woi=\(woiPrefs)")
"&channel=beta&version=\(appVersion)&daily=true&weekly=true&monthly=true&first=false&woi=\(woiPrefs)")
}

func testArbitraryWoiDate() {
Expand All @@ -105,7 +105,7 @@ class DauTest: XCTestCase {
let defaultDate = DAU.defaultWoiDate

XCTAssertEqual(params!,
"&channel=beta&version=\(AppInfo.appVersion)&daily=true&weekly=true&monthly=true&first=false&woi=\(defaultDate)")
"&channel=beta&version=\(appVersion)&daily=true&weekly=true&monthly=true&first=false&woi=\(defaultDate)")

XCTAssertNotNil(prefs.stringForKey(DAU.weekOfInstallationKeyPrefKey))
}
Expand Down Expand Up @@ -133,6 +133,17 @@ class DauTest: XCTestCase {
XCTAssertEqual(sunday.weeksMonday, "2017-11-27")
}

func testAppend0ToAppVersion() {
XCTAssertFalse(DAU.shouldAppend0ToAppVersion("1.5.2"))
XCTAssertFalse(DAU.shouldAppend0ToAppVersion("1.52.2"))
XCTAssertFalse(DAU.shouldAppend0ToAppVersion("11.5.23"))
XCTAssertFalse(DAU.shouldAppend0ToAppVersion("11.55.23"))

XCTAssertTrue(DAU.shouldAppend0ToAppVersion("1.5"))
XCTAssertTrue(DAU.shouldAppend0ToAppVersion("11.5"))
XCTAssertTrue(DAU.shouldAppend0ToAppVersion("1.10"))
}

private func componentsOfDate(_ dateString: String) -> DateComponents {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
Expand All @@ -148,4 +159,8 @@ class DauTest: XCTestCase {

return dateFormatter.date(from: string)!
}

private var appVersion: String {
return DAU.shouldAppend0ToAppVersion(AppInfo.appVersion) ? AppInfo.appVersion + ".0" : AppInfo.appVersion
}
}

0 comments on commit 151a7d3

Please sign in to comment.