BuzzFeed Tech

Sharing our experiences & discoveries for the betterment of all!

Follow publication

Architecting Trust in Client-Side Analytics, Part One

David Mauro
BuzzFeed Tech
Published in
6 min readApr 3, 2017

Data powers a rapid learning cycle that informs both the product and the content

Architecting trust

protocol EventObservable {
associatedtype EventType
func track(event: EventType) -> ReceiptProtocol?
}
protocol ReceiptProtocol {
var eventName: String { get }
var providerName: String { get }
var timestamp: Date { get }
var metadata: [String: Any] { get }
}
enum Event {
case sessionStart
case screenView(ScreenType)
case share(URL, String)
}
enum ScreenType {
case home
case settings
}

Putting it all together

private extension ScreenType {
var gaScreenName: String {
switch self {
case .home:
return "home"
case .settings:
return "settings"
}
}
}
struct GoogleAnalyticsObserver: EventObservable {
func track(event: Event) -> ReceiptProtocol? {
switch event {
case let .screenView(screenType):
trackScreenView(for: screenType.gaScreenName)
case .sessionStart:
// Don’t use default: so that when new events are added
// we are forced to decide what to do with them here.
break
}
}

private func trackScreenView(for name: String) {
// Implementation details of this are not important
}
}
class AnalyticsController<EventType> {
private var observers = [ObserverThunk<EventType>]()
private var eventReceipts = [ReceiptProtocol]()
func track(event: EventType) {
for observer in observers {
if let receipt = observer.track(event: event) {
// Prepend for default reverse chron
eventReceipts.insert(receipt, atIndex: 0)
}
}
}

func add<U: EventObservable where U.EventType == EventType>(observer: U) {
let thunk = ObserverThunk(observer)
observers.append(thunk)
}
}
let analyticsController = AnalyticsController<Event>()
analyticsController.add(observer: GoogleAnalyticsObserver())
analyticsController.track(event: .sessionStart)
analyticsController.track(event: .screenView(.home))

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in BuzzFeed Tech

Sharing our experiences & discoveries for the betterment of all!

Written by David Mauro

Engineering Manager for BuzzFeed’s Apps Team

No responses yet

Write a response