-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-ssr.js
108 lines (103 loc) · 3.3 KB
/
gatsby-ssr.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
107
108
import React from "react"
const knownOptions = {
clientId: `string`,
sampleRate: `number`,
siteSpeedSampleRate: `number`,
alwaysSendReferrer: `boolean`,
allowAnchor: `boolean`,
cookieName: `string`,
cookieExpires: `number`,
storeGac: `boolean`,
legacyCookieDomain: `string`,
legacyHistoryImport: `boolean`,
allowLinker: `boolean`,
}
exports.onRenderBody = (
{ setHeadComponents, setPostBodyComponents },
pluginOptions
) => {
if (process.env.NODE_ENV === `production`) {
let excludeGAPaths = []
if (typeof pluginOptions.exclude !== `undefined`) {
const Minimatch = require(`minimatch`).Minimatch
pluginOptions.exclude.map(exclude => {
const mm = new Minimatch(exclude)
excludeGAPaths.push(mm.makeRe())
})
}
const gaCreateOptions = {}
for (const option in knownOptions) {
if (typeof pluginOptions[option] === knownOptions[option]) {
gaCreateOptions[option] = pluginOptions[option]
}
}
const setComponents = pluginOptions.head
? setHeadComponents
: setPostBodyComponents
return setComponents([
<script
key={`gatsby-plugin-google-analytics`}
dangerouslySetInnerHTML={{
__html: `
${
excludeGAPaths.length
? `window.excludeGAPaths=[${excludeGAPaths.join(`,`)}];`
: ``
}
${
typeof pluginOptions.anonymize !== `undefined` &&
pluginOptions.anonymize === true
? `function gaOptout(){document.cookie=disableStr+'=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;path=/',window[disableStr]=!0}var gaProperty='${
pluginOptions.trackingId
}',disableStr='ga-disable-'+gaProperty;document.cookie.indexOf(disableStr+'=true')>-1&&(window[disableStr]=!0);`
: ``
}
if(${
typeof pluginOptions.respectDNT !== `undefined` &&
pluginOptions.respectDNT == true
? `!(navigator.doNotTrack == "1" || window.doNotTrack == "1")`
: `true`
}) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
}
if (typeof ga === "function") {
ga('create', '${pluginOptions.trackingId}', '${
typeof pluginOptions.cookieDomain === `string`
? pluginOptions.cookieDomain
: `auto`
}', ${
typeof pluginOptions.name === `string`
? `'${pluginOptions.name}', `
: ``
}${JSON.stringify(gaCreateOptions)});
${
typeof pluginOptions.anonymize !== `undefined` &&
pluginOptions.anonymize === true
? `ga('set', 'anonymizeIp', true);`
: ``
}
${
typeof pluginOptions.optimizeId !== `undefined`
? `ga('require', '${pluginOptions.optimizeId}');`
: ``
}
${
typeof pluginOptions.experimentId !== `undefined`
? `ga('set', 'expId', '${pluginOptions.experimentId}');`
: ``
}
${
typeof pluginOptions.variationId !== `undefined`
? `ga('set', 'expVar', '${pluginOptions.variationId}');`
: ``
}}
`,
}}
/>,
])
}
return null
}