How to Avoid Embedding Google Adsense and Analytics Code in Your Development Environment on VuePress

Last Updated: 2022/02/11 10:08:34

You can avoid embedding some scripts by modifying src/.vuepress/config.js.

const config = {
	// ...

	// head
	// see more detail on https://vuepress.vuejs.org/config/#head
	head: [
		// ...
	], 
	// ...
}

// `process.env.NODE_ENV` will be "production" or "development" depending on your environment.
if (process.env.NODE_ENV && process.env.NODE_ENV == "production") {
	config.head = config.head || [];

	// example for google adsense
	config.head.push(['script',{ 
		async: true, 
		src: 'https://pagead2.googlesyndication.com/...', 
		crossorigin: "anonymous", 
	}]);

	// example of raw javascript code, such as Google Analytics
	config.head.push(['script', {}, `
		// inner code of <script>...</script>
	`]);
}

// export
module.exports = config
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

# Relevant Documentations

Last Updated: 2022/02/11 10:08:34
Copyright © Web Ninja All Rights Reserved.