forked from julianlam/nodebb-plugin-google-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (74 loc) · 2.11 KB
/
index.js
File metadata and controls
87 lines (74 loc) · 2.11 KB
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
var fs = require('fs'),
path = require('path'),
winston = module.parent.require('winston'),
db = module.parent.require('./database'),
GA = {};
GA.init = function(callback) {
if (GA.serverGA !== undefined) return callback();
db.getObjectFields('config', ['ga:id', 'ga:domain'], function(err, options) {
if (!err && options['ga:id']/* && options['ga:domain']*/) {
GA.id = options['ga:id'];
// GA.domain = options['ga:domain'];
callback();
} else {
callback(new Error('A Google Analytics ID (e.g. UA-XXXXX-X) was not specified.'));
}
});
}
GA.addTrackingScript = function(appendHTML, callback) {
GA.init(function(err) {
if (!err) {
appendHTML += "\
<script type=\"text/javascript\"> \
var _gaq = _gaq || []; \
_gaq.push(['_setAccount', '" + GA.id + "']); \
_gaq.push(['_trackPageview']); \
\
(function() { \
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; \
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; \
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); \
})(); \
</script> \
";
callback(err, appendHTML);
} else {
winston.error(err.message);
callback(err, appendHTML);
}
});
};
GA.loadScript = function(scripts) {
return scripts.concat([
'plugins/nodebb-plugin-google-analytics/listener.js'
]);
};
GA.admin = {
menu: function(custom_header, callback) {
custom_header.plugins.push({
"route": '/plugins/google-analytics',
"icon": 'fa-bar-chart-o',
"name": 'Google Analytics'
});
return custom_header;
},
route: function(custom_routes, callback) {
fs.readFile(path.join(__dirname, 'admin.tpl'), function(err, tpl) {
custom_routes.routes.push({
route: '/plugins/google-analytics',
method: "get",
options: function(req, res, callback) {
callback({
req: req,
res: res,
route: '/plugins/google-analytics',
name: 'Google Analytics',
content: tpl
});
}
});
callback(null, custom_routes);
});
}
};
module.exports = GA;