@@ -18,31 +18,43 @@ exports.onRenderBody = (
18
18
{ setHeadComponents, setPostBodyComponents } ,
19
19
pluginOptions
20
20
) => {
21
- if ( process . env . NODE_ENV === `production` ) {
22
- let excludeGAPaths = [ ]
23
- if ( typeof pluginOptions . exclude !== `undefined` ) {
24
- const Minimatch = require ( `minimatch` ) . Minimatch
25
- pluginOptions . exclude . map ( exclude => {
26
- const mm = new Minimatch ( exclude )
27
- excludeGAPaths . push ( mm . makeRe ( ) )
28
- } )
29
- }
21
+ if ( process . env . NODE_ENV !== `production` ) {
22
+ return null
23
+ }
30
24
31
- const gaCreateOptions = { }
32
- for ( const option in knownOptions ) {
33
- if ( typeof pluginOptions [ option ] === knownOptions [ option ] ) {
34
- gaCreateOptions [ option ] = pluginOptions [ option ]
35
- }
25
+ // Lighthouse recommends pre-connecting to google analytics
26
+ setHeadComponents ( [
27
+ < link
28
+ rel = "preconnect dns-prefetch"
29
+ key = "preconnect-google-analytics"
30
+ href = "https://www.google-analytics.com"
31
+ /> ,
32
+ ] )
33
+
34
+ let excludeGAPaths = [ ]
35
+ if ( typeof pluginOptions . exclude !== `undefined` ) {
36
+ const Minimatch = require ( `minimatch` ) . Minimatch
37
+ pluginOptions . exclude . map ( exclude => {
38
+ const mm = new Minimatch ( exclude )
39
+ excludeGAPaths . push ( mm . makeRe ( ) )
40
+ } )
41
+ }
42
+
43
+ const gaCreateOptions = { }
44
+ for ( const option in knownOptions ) {
45
+ if ( typeof pluginOptions [ option ] === knownOptions [ option ] ) {
46
+ gaCreateOptions [ option ] = pluginOptions [ option ]
36
47
}
48
+ }
37
49
38
- const setComponents = pluginOptions . head
39
- ? setHeadComponents
40
- : setPostBodyComponents
41
- return setComponents ( [
42
- < script
43
- key = { `gatsby-plugin-google-analytics` }
44
- dangerouslySetInnerHTML = { {
45
- __html : `
50
+ const setComponents = pluginOptions . head
51
+ ? setHeadComponents
52
+ : setPostBodyComponents
53
+ return setComponents ( [
54
+ < script
55
+ key = { `gatsby-plugin-google-analytics` }
56
+ dangerouslySetInnerHTML = { {
57
+ __html : `
46
58
${
47
59
excludeGAPaths . length
48
60
? `window.excludeGAPaths=[${ excludeGAPaths . join ( `,` ) } ];`
@@ -69,14 +81,14 @@ exports.onRenderBody = (
69
81
}
70
82
if (typeof ga === "function") {
71
83
ga('create', '${ pluginOptions . trackingId } ', '${
72
- typeof pluginOptions . cookieDomain === `string`
73
- ? pluginOptions . cookieDomain
74
- : `auto`
75
- } ', ${
76
- typeof pluginOptions . name === `string`
77
- ? `'${ pluginOptions . name } ', `
78
- : ``
79
- } ${ JSON . stringify ( gaCreateOptions ) } );
84
+ typeof pluginOptions . cookieDomain === `string`
85
+ ? pluginOptions . cookieDomain
86
+ : `auto`
87
+ } ', ${
88
+ typeof pluginOptions . name === `string`
89
+ ? `'${ pluginOptions . name } ', `
90
+ : ``
91
+ } ${ JSON . stringify ( gaCreateOptions ) } );
80
92
${
81
93
typeof pluginOptions . anonymize !== `undefined` &&
82
94
pluginOptions . anonymize === true
@@ -99,10 +111,7 @@ exports.onRenderBody = (
99
111
: ``
100
112
} }
101
113
` ,
102
- } }
103
- /> ,
104
- ] )
105
- }
106
-
107
- return null
114
+ } }
115
+ /> ,
116
+ ] )
108
117
}
0 commit comments