File size: 2,428 Bytes
e18c302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const Amplify = window.aws_amplify.Amplify
const Analytics = Amplify.Analytics
const KinesisFirehoseProvider = window.aws_amplify.AWSKinesisFirehoseProvider

const awsconfig = {
	"aws_project_region": "eu-west-1",
	"aws_cognito_identity_pool_id": "eu-west-1:3df3caec-4bb6-4891-b154-ee940c8264b8",
	"aws_cognito_region": "eu-west-1",
	"aws_kinesis_firehose_stream_name": "ClickStreamKinesisFirehose-OGX7PQdrynUo",
};

const RUNTIME = "python"
const BASE_ORIGIN = "docs.powertools.aws.dev"

function copyToClipboard(e) {
	e.preventDefault()
	navigator.clipboard.writeText(e.target.textContent)
  	alert$.next("Copied to clipboard")
}

function enableSearchOnBlurElement() {
	if (document.location.hostname != BASE_ORIGIN) return // prevent unnecessary data
	/* Register handler to log search on blur */
	document.addEventListener("DOMContentLoaded", function () {
		recordPageView({
			prevLocation: document.referrer
		})
		if (document.forms.search) {
			let query = document.forms.search.query
			query.addEventListener("blur", function () {
				// If Search result is ever actionable
				// we should populate `value`
				if (this.value) {
					let path = document.location.pathname;
					console.info(`Search value: ${this.value}`)
					recordPageView({
						searchPattern: this.value
					})
				}
			})
		}
	})

	// Register handler for page sections when browser history is changed
	window.onpopstate = function (event) {
		recordPageView({
			prevLocation: document.referrer
		})
	};
}

function enableClipboardElements() {
	let copyElements = document.querySelectorAll('.copyMe');
	copyElements.forEach(element => {
		element.addEventListener('click', copyToClipboard);
	})
}

const attachListeners = () => {
	enableSearchOnBlurElement()
	enableClipboardElements()
}

const init = () => {
	Analytics.addPluggable(new KinesisFirehoseProvider())
	Amplify.configure(awsconfig);

	Analytics.configure({
		AWSKinesisFirehose: {
			region: awsconfig.aws_project_region
		}
	})

	attachListeners()
}

const recordPageView = ({prevLocation, searchPattern}) => {
	Analytics.record({
		data: {
			// Do not count page view for search
			url: searchPattern ? null : window.location.href,
			section: searchPattern ? null : location.pathname,
			previous: prevLocation || null,
			search: searchPattern || null,
			language: RUNTIME
		},
		streamName: awsconfig.aws_kinesis_firehose_stream_name
	}, 'AWSKinesisFirehose')
}

init()