-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
57 lines (47 loc) · 1.32 KB
/
plugin.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
/**
* We.js google analytics plugin main file
*/
module.exports = function loadPlugin(projectPath, Plugin) {
const plugin = new Plugin(__dirname);
// plugin configs
plugin.setConfigs({
GA: {
id: null,
env: {
prod: true,
dev: false,
test: false
}
}
});
plugin.addGoogleAnalytcsTag = function addGoogleAnalytcsTag(data) {
let id = plugin.getGoogleAnalyticsID();
if (!id) return;
data.html.text += `<script async src="https://www.googletagmanager.com/gtag/js?id=${id}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${id}');
</script>`;
}
/**
* Get google analytics id from DB systemSettings or from plugin.we.config.GA.id
*
* @return {Number|Null}
*/
plugin.getGoogleAnalyticsID = function getGoogleAnalyticsID() {
if (
plugin.we.systemSettings &&
plugin.we.systemSettings.googleAnalyticsID
) {
return plugin.we.systemSettings.googleAnalyticsID;
}
if (plugin.we.config.GA.env[plugin.we.env] && plugin.we.config.GA.id) {
return plugin.we.config.GA.id;
}
return null;
}
plugin.events.on('we-html-head-start', plugin.addGoogleAnalytcsTag);
return plugin;
};