-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
index.js
106 lines (96 loc) · 2.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { Component } from 'react'
import analytics from '../../utils/analytics'
import Log from '../../components/Log'
import Navigation from '../../fragments/Nav'
export default class App extends Component {
constructor (props, context) {
super(props, context)
this.state = {
history: window.__analytics__ || []
}
}
componentDidMount() {
this.listener = analytics.on('*', ({ payload }) => {
this.setState({
history: window.__analytics__.concat(payload)
})
})
this.sync = setInterval(() => {
this.setState({
history: window.__analytics__
})
}, 1000)
}
componentWillUnmount() {
// remove listener
this.listener()
}
handleOptOut = () => {
analytics.identify('xyz-123', {
optOut: true
})
}
clearLog = () => {
// Clear previous history
clearInterval(this.sync)
this.listener()
this.setState({
history: []
}, () => {
// re-attach listener to show history
this.listener = analytics.on('*', ({ payload }) => {
this.setState({
history: this.state.history.concat(payload)
})
})
})
}
renderProviders() {
const plugins = analytics.getState('plugins')
return Object.keys(plugins).map((name, i) => {
const disable = () => {
analytics.disablePlugin([name])
}
const enable = () => {
analytics.enablePlugin([name])
}
const word = (plugins[name].enabled) ? 'off' : 'on'
const handler = (plugins[name].enabled) ? disable : enable
return (
<button onClick={handler} key={i}>
Turn {word} plugin {name}
</button>
)
})
}
render() {
const { history } = this.state
return (
<div className="app">
<Navigation />
<h2 className="kitchen-sink-title">Analytics Privacy & GDPR Features
<button onClick={this.clearLog}>
Clear logs
</button>
</h2>
<div className='section-wrapper'>
<div className='section'>
<h3>Opt out of all tracking</h3>
<button onClick={this.handleOptOut}>
Visitor Opt out
</button>
</div>
</div>
<div className='section-wrapper'>
<div className='section'>
<h3>Allow visitors to choose analytics</h3>
{this.renderProviders()}
</div>
<div className='section'>
</div>
</div>
<Log items={history} />
</div>
)
}
}