// @flow
import {
	GoogleAnalyticsTracker,
	GoogleAnalyticsSettings,
} from 'react-native-google-analytics-bridge'
import {stringifyFilters} from './views/components/filter'

import {getAnalyticsOptOut} from './lib/storage'

const trackerId =
	process.env.NODE_ENV === 'development' ? 'UA-90234209-1' : 'UA-90234209-2'

export const tracker = new GoogleAnalyticsTracker(trackerId)

function disableIfOptedOut() {
	return getAnalyticsOptOut().then(didOptOut => {
		if (didOptOut) {
			GoogleAnalyticsSettings.setOptOut(true)
		}
	})
}
disableIfOptedOut()

// Google requires that custom dimensions be tracked by index, and we only get
// 20 custom dimensions, so I decided to centralize them here.
export function trackMenuFilters(menuName: string, filters: any) {
	tracker.trackEventWithCustomDimensionValues(
		'menus',
		'filter',
		{label: menuName},
		{'1': stringifyFilters(filters)},
	)
}

export function trackHomescreenOrder(order: string[]) {
	tracker.trackEventWithCustomDimensionValues(
		'homescreen',
		'reorder',
		{},
		{'2': order.join(', ')},
	)
}

export function trackStreamPlay(streamName: string) {
	tracker.trackEvent('stream', 'play', streamName)
}

export function trackStreamPause(streamName: string) {
	tracker.trackEvent('stream', 'pause', streamName)
}

export function trackStreamError(streamName: string) {
	tracker.trackEvent('stream', 'error', streamName)
}

export const trackBuildingView = (buildingName: string) =>
	tracker.trackEvent('building-hours', 'open', {label: buildingName})

export const trackDefinitionView = (word: string) =>
	tracker.trackEvent('dictionary', 'open', {label: word})

export const trackOrgOpen = (orgName: string) =>
	tracker.trackEvent('student-org', 'open', orgName)

@@ -1067,32 +1067,574 @@
 export function 
+trackHomescreenDisabledItem(viewName: string) %7B%0A%09tracker.trackEventWithCustomDimensionValues(%0A%09%09'homescreen',%0A%09%09'disabled',%0A%09%09%7Blabel: viewName, value: 1%7D,%0A%09%09%7B'3': viewName%7D,%0A%09)%0A%7D%0A%0Aexport function trackHomescreenReenabledItem(viewName: string) %7B%0A%09tracker.trackEventWithCustomDimensionValues(%0A%09%09'homescreen',%0A%09%09're-enabled',%0A%09%09%7Blabel: viewName, value: -1%7D,%0A%09%09%7B'3': viewName%7D,%0A%09)%0A%7D%0A%0A// These are centralized event functions, so we have an easy place to review%0A// them all. There is no limit to the number of these that we can have.%0Aexport const 
 trackStreamPlay(
@@ -1632,16 +1632,19 @@
 reamPlay
+ = 
 (streamN
@@ -1648,33 +1648,34 @@
 amName: string) 
-%7B
+=%3E
 %0A%09tracker.trackE
@@ -1696,16 +1696,24 @@
  'play',
+ %7Blabel:
  streamN
@@ -1707,35 +1707,34 @@
 abel: streamName
-)%0A
 %7D
+)
 %0A%0Aexport functio
@@ -1722,32 +1722,29 @@
 e%7D)%0A%0Aexport 
-function
+const
  trackStream
@@ -1748,16 +1748,19 @@
 eamPause
+ = 
 (streamN
@@ -1764,33 +1764,34 @@
 amName: string) 
-%7B
+=%3E
 %0A%09tracker.trackE
@@ -1813,16 +1813,24 @@
 'pause',
+ %7Blabel:
  streamN
@@ -1824,35 +1824,34 @@
 abel: streamName
+%7D
 )
-%0A%7D
 %0A%0Aexport functio
@@ -1839,32 +1839,29 @@
 e%7D)%0A%0Aexport 
-function
+const
  trackStream
@@ -1865,16 +1865,19 @@
 eamError
+ = 
 (streamN
@@ -1881,33 +1881,34 @@
 amName: string) 
-%7B
+=%3E
 %0A%09tracker.trackE
@@ -1930,16 +1930,24 @@
 'error',
+ %7Blabel:
  streamN
@@ -1949,19 +1949,18 @@
 reamName
-)%0A
 %7D
+)
 %0A%0Aexport

add two new custom-dimension ga trackers