Skip to content

Commit

Permalink
Add some usage analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBennet committed Mar 16, 2019
1 parent 73d42cd commit b4cf9a6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
30 changes: 15 additions & 15 deletions ReadingList/Data/UserEngagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ class UserEngagement {
// Note: TestFlight users are automatically enrolled in analytics and crash reporting. This should be reflected
// on the corresponding Settings page.
static var sendAnalytics: Bool {
#if DEBUG
return false
#else
return BuildInfo.appConfiguration == .testFlight || UserDefaults.standard[.sendAnalytics]
#endif
}

static var sendCrashReports: Bool {
#if DEBUG
return false
#else
return BuildInfo.appConfiguration == .testFlight || UserDefaults.standard[.sendCrashReports]
#endif
}

static func initialiseUserAnalytics() {
#if RELEASE
// We need to configure the firebase app in order to send crash reports
if sendAnalytics || sendCrashReports { FirebaseApp.configure() }
if sendCrashReports { Fabric.with([Crashlytics.self]) }
#endif
guard sendAnalytics || sendCrashReports else { return }
// We need to configure the firebase app in order to send crash reports
if FirebaseApp.app() == nil {
FirebaseApp.configure()
}
if sendCrashReports {
#if RELEASE
Fabric.with([Crashlytics.self])
#endif
}
}

static func onReviewTrigger() {
Expand Down Expand Up @@ -60,6 +57,7 @@ class UserEngagement {
case bulkDeleteBook = "Bulk_Delete_Book"
case editBook = "Edit_Book"
case editReadState = "Edit_Read_State"
case changeSortOrder = "Change_Sort"

// Lists
case createList = "Create_List"
Expand All @@ -68,7 +66,7 @@ class UserEngagement {
case removeBookFromList = "Remove_Book_From_List"
case reorederList = "Reorder_List"
case deleteList = "Delete_List"
case setListOrder = "Set_List_Order"
case changeListSortOrder = "Change_List_Sort_Order"
case renameList = "Rename_List"

// Quick actions
Expand All @@ -81,7 +79,7 @@ class UserEngagement {
case disableCrashReports = "Disable_Crash_Reports"
case enableCrashReports = "Enable_Crash_Reports"
case changeTheme = "Change_Theme"
case changeSortOrder = "Change_Sort"
case changeSearchOnlineLanguage = "Change_Search_Online_Language"

// Other
case viewOnAmazon = "View_On_Amazon"
Expand All @@ -90,7 +88,9 @@ class UserEngagement {

static func logEvent(_ event: Event) {
guard sendAnalytics else { return }
#if RELEASE
Analytics.logEvent(event.rawValue, parameters: nil)
#endif
}

static func logError(_ error: Error) {
Expand Down
1 change: 1 addition & 0 deletions ReadingList/ViewControllers/Lists/AddToExistingLists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ class AddToExistingLists: UITableViewController {
list.addBooks(NSOrderedSet(set: self.books))
}
navigationController?.dismiss(animated: true, completion: onComplete)
UserEngagement.logEvent(.addBookToList)
}
}
1 change: 1 addition & 0 deletions ReadingList/ViewControllers/Lists/ListBookTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class ListBookTable: UITableViewController {

// Put the top row at the "middle", so that the top row is not right up at the top of the table
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .middle, animated: false)
UserEngagement.logEvent(.changeListSortOrder)
}

@objc private func objectContextChanged(_ notification: Notification) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class RemoveFromExistingLists: UITableViewController {
let list = self.resultsController.object(at: indexPath)
list.removeBooks(NSSet(object: self.book))
list.managedObjectContext!.saveAndLogIfErrored()
UserEngagement.logEvent(.removeBookFromList)
}]
}
}
Expand Down
1 change: 1 addition & 0 deletions ReadingList/ViewControllers/Main/BookTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class BookTable: UITableViewController { //swiftlint:disable:this type_body_leng
header.onSortChanged = { [unowned self] in
self.buildResultsController()
self.tableView.reloadData()
UserEngagement.logEvent(.changeSortOrder)
}
configureHeader(header, at: section)
return header
Expand Down
3 changes: 3 additions & 0 deletions ReadingList/ViewControllers/Settings/General.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class General: FormViewController {
} else {
UserDefaults.standard[.searchLanguageRestriction] = nil
}
UserEngagement.logEvent(.changeSearchOnlineLanguage)
}
}

Expand Down Expand Up @@ -81,6 +82,7 @@ class General: FormViewController {
guard let switchValue = sender.value else { return }
if switchValue {
UserDefaults.standard[.sendCrashReports] = true
UserEngagement.initialiseUserAnalytics()
UserEngagement.logEvent(.enableCrashReports)
} else {
// If this is being turned off, let's try to persuade them to turn it back on
Expand All @@ -104,6 +106,7 @@ class General: FormViewController {
guard let switchValue = sender.value else { return }
if switchValue {
UserDefaults.standard[.sendAnalytics] = true
UserEngagement.initialiseUserAnalytics()
UserEngagement.logEvent(.enableAnalytics)
} else {
// If this is being turned off, let's try to persuade them to turn it back on
Expand Down

0 comments on commit b4cf9a6

Please sign in to comment.