backend-manager 3.2.169 → 3.2.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/dist/cli/cli.js +1534 -0
- package/dist/manager/functions/core/actions/api/admin/backup.js +338 -0
- package/dist/manager/functions/core/actions/api/admin/create-post.js +388 -0
- package/dist/manager/functions/core/actions/api/admin/cron.js +37 -0
- package/dist/manager/functions/core/actions/api/admin/database-read.js +35 -0
- package/dist/manager/functions/core/actions/api/admin/database-write.js +39 -0
- package/dist/manager/functions/core/actions/api/admin/edit-post.js +158 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-query.js +165 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-read.js +38 -0
- package/dist/manager/functions/core/actions/api/admin/firestore-write.js +54 -0
- package/dist/manager/functions/core/actions/api/admin/get-stats.js +269 -0
- package/dist/manager/functions/core/actions/api/admin/payment-processor.js +57 -0
- package/dist/manager/functions/core/actions/api/admin/run-hook.js +95 -0
- package/dist/manager/functions/core/actions/api/admin/send-notification.js +197 -0
- package/dist/manager/functions/core/actions/api/admin/sync-users.js +125 -0
- package/dist/manager/functions/core/actions/api/admin/templates/post.html +16 -0
- package/dist/manager/functions/core/actions/api/firebase/get-providers.js +102 -0
- package/dist/manager/functions/core/actions/api/general/emails/general:download-app-link.js +21 -0
- package/dist/manager/functions/core/actions/api/general/fetch-post.js +99 -0
- package/dist/manager/functions/core/actions/api/general/generate-uuid.js +41 -0
- package/dist/manager/functions/core/actions/api/general/send-email.js +112 -0
- package/dist/manager/functions/core/actions/api/handler/create-post.js +146 -0
- package/dist/manager/functions/core/actions/api/special/setup-electron-manager-client.js +103 -0
- package/dist/manager/functions/core/actions/api/template.js +33 -0
- package/dist/manager/functions/core/actions/api/test/authenticate.js +22 -0
- package/dist/manager/functions/core/actions/api/test/create-test-accounts.js +27 -0
- package/dist/manager/functions/core/actions/api/test/lab.js +55 -0
- package/dist/manager/functions/core/actions/api/test/redirect.js +26 -0
- package/dist/manager/functions/core/actions/api/test/webhook.js +30 -0
- package/dist/manager/functions/core/actions/api/user/create-custom-token.js +32 -0
- package/dist/manager/functions/core/actions/api/user/delete.js +68 -0
- package/dist/manager/functions/core/actions/api/user/get-active-sessions.js +45 -0
- package/dist/manager/functions/core/actions/api/user/get-subscription-info.js +49 -0
- package/dist/manager/functions/core/actions/api/user/oauth2/discord.js +114 -0
- package/dist/manager/functions/core/actions/api/user/oauth2/google.js +99 -0
- package/dist/manager/functions/core/actions/api/user/oauth2.js +476 -0
- package/dist/manager/functions/core/actions/api/user/regenerate-api-keys.js +54 -0
- package/dist/manager/functions/core/actions/api/user/resolve.js +32 -0
- package/dist/manager/functions/core/actions/api/user/sign-out-all-sessions.js +118 -0
- package/dist/manager/functions/core/actions/api/user/sign-up copy.js +544 -0
- package/dist/manager/functions/core/actions/api/user/sign-up.js +99 -0
- package/dist/manager/functions/core/actions/api/user/submit-feedback.js +96 -0
- package/dist/manager/functions/core/actions/api/user/validate-settings.js +86 -0
- package/dist/manager/functions/core/actions/api.js +354 -0
- package/dist/manager/functions/core/actions/create-post-handler.js +184 -0
- package/dist/manager/functions/core/actions/generate-uuid.js +62 -0
- package/dist/manager/functions/core/actions/sign-up-handler.js +205 -0
- package/dist/manager/functions/core/admin/create-post.js +206 -0
- package/dist/manager/functions/core/admin/firestore-write.js +72 -0
- package/dist/manager/functions/core/admin/get-stats.js +218 -0
- package/dist/manager/functions/core/admin/query.js +198 -0
- package/dist/manager/functions/core/admin/send-notification.js +206 -0
- package/dist/manager/functions/core/cron/daily/ghostii-auto-publisher.js +377 -0
- package/dist/manager/functions/core/cron/daily/reset-usage.js +197 -0
- package/dist/manager/functions/core/cron/daily.js +114 -0
- package/dist/manager/functions/core/events/auth/before-create.js +124 -0
- package/dist/manager/functions/core/events/auth/before-signin.js +62 -0
- package/dist/manager/functions/core/events/auth/on-create copy.js +121 -0
- package/dist/manager/functions/core/events/auth/on-create.js +564 -0
- package/dist/manager/functions/core/events/auth/on-delete.js +72 -0
- package/dist/manager/functions/core/events/firestore/on-subscription.js +107 -0
- package/dist/manager/functions/test/authenticate.js +38 -0
- package/dist/manager/functions/test/create-test-accounts.js +144 -0
- package/dist/manager/functions/test/webhook.js +37 -0
- package/dist/manager/functions/wrappers/mailchimp/addToList.js +25 -0
- package/dist/manager/helpers/analytics copy.js +217 -0
- package/dist/manager/helpers/analytics.js +467 -0
- package/dist/manager/helpers/api-manager.js +324 -0
- package/dist/manager/helpers/assistant.js +1043 -0
- package/dist/manager/helpers/metadata.js +32 -0
- package/dist/manager/helpers/middleware.js +154 -0
- package/dist/manager/helpers/roles.js +69 -0
- package/dist/manager/helpers/settings.js +158 -0
- package/dist/manager/helpers/subscription-resolver-new.js +828 -0
- package/dist/manager/helpers/subscription-resolver.js +842 -0
- package/dist/manager/helpers/usage.js +381 -0
- package/dist/manager/helpers/user.js +198 -0
- package/dist/manager/helpers/utilities.js +292 -0
- package/dist/manager/index.js +1076 -0
- package/dist/manager/libraries/openai.js +460 -0
- package/dist/manager/routes/restart/index.js +52 -0
- package/dist/manager/routes/test/index.js +43 -0
- package/dist/manager/schemas/restart.js +13 -0
- package/dist/manager/schemas/test.js +13 -0
- package/dist/require.js +3 -0
- package/package.json +19 -9
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
function Module() {
|
|
2
|
+
const self = this;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
Module.prototype.init = function (Manager, payload) {
|
|
6
|
+
const self = this;
|
|
7
|
+
self.Manager = Manager;
|
|
8
|
+
self.libraries = Manager.libraries;
|
|
9
|
+
self.assistant = Manager.Assistant();
|
|
10
|
+
self.change = payload.change
|
|
11
|
+
self.context = payload.context
|
|
12
|
+
|
|
13
|
+
return self;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
Module.prototype.main = function () {
|
|
17
|
+
const self = this;
|
|
18
|
+
const libraries = self.libraries;
|
|
19
|
+
const assistant = self.assistant;
|
|
20
|
+
const change = self.change;
|
|
21
|
+
const context = self.context;
|
|
22
|
+
|
|
23
|
+
return new Promise(async function(resolve, reject) {
|
|
24
|
+
const _ = self.Manager.require('lodash');
|
|
25
|
+
|
|
26
|
+
const dataBefore = change.before.data();
|
|
27
|
+
const dataAfter = change.after.data();
|
|
28
|
+
|
|
29
|
+
let analytics;
|
|
30
|
+
let eventType;
|
|
31
|
+
|
|
32
|
+
if (dataAfter == undefined) {
|
|
33
|
+
eventType = 'delete';
|
|
34
|
+
} else if (dataBefore && dataAfter) {
|
|
35
|
+
eventType = 'update';
|
|
36
|
+
} else if (!dataBefore && dataAfter) {
|
|
37
|
+
eventType = 'create';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
assistant.log('Notification subscription write:', {
|
|
41
|
+
after: dataAfter,
|
|
42
|
+
before: dataBefore,
|
|
43
|
+
eventType: eventType,
|
|
44
|
+
resource: context.resource,
|
|
45
|
+
params: context.params,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Delete event
|
|
49
|
+
if (eventType === 'delete') {
|
|
50
|
+
await libraries.admin.firestore().doc(`meta/stats`)
|
|
51
|
+
.update({
|
|
52
|
+
'subscriptions.total': libraries.admin.firestore.FieldValue.increment(-1),
|
|
53
|
+
})
|
|
54
|
+
.then(r => {
|
|
55
|
+
analytics = self.Manager.Analytics({
|
|
56
|
+
assistant: assistant,
|
|
57
|
+
uuid: _.get(dataBefore, 'link.user.data.uid', undefined),
|
|
58
|
+
})
|
|
59
|
+
.event({
|
|
60
|
+
name: 'notification-unsubscribe',
|
|
61
|
+
params: {},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
assistant.log('Notification subscription deleted:', dataBefore);
|
|
65
|
+
|
|
66
|
+
return resolve(dataBefore);
|
|
67
|
+
})
|
|
68
|
+
.catch(e => {
|
|
69
|
+
assistant.error(e);
|
|
70
|
+
return reject(e);
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
// Update event
|
|
74
|
+
} else if (eventType === 'update') {
|
|
75
|
+
return resolve();
|
|
76
|
+
|
|
77
|
+
// Create event
|
|
78
|
+
} else if (eventType === 'create') {
|
|
79
|
+
await libraries.admin.firestore().doc(`meta/stats`)
|
|
80
|
+
.update({
|
|
81
|
+
'subscriptions.total': libraries.admin.firestore.FieldValue.increment(1),
|
|
82
|
+
})
|
|
83
|
+
.then(r => {
|
|
84
|
+
analytics = self.Manager.Analytics({
|
|
85
|
+
assistant: assistant,
|
|
86
|
+
uuid: _.get(dataAfter, 'link.user.data.uid', undefined),
|
|
87
|
+
})
|
|
88
|
+
.event({
|
|
89
|
+
name: 'notification-subscribe',
|
|
90
|
+
params: {},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
assistant.log('Notification subscription created:', dataAfter);
|
|
94
|
+
|
|
95
|
+
return resolve(dataAfter);
|
|
96
|
+
})
|
|
97
|
+
.catch(e => {
|
|
98
|
+
assistant.error(e);
|
|
99
|
+
return reject(e);
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
module.exports = Module;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
let Module = {
|
|
2
|
+
init: async function (Manager, data) {
|
|
3
|
+
this.Manager = Manager;
|
|
4
|
+
this.libraries = Manager.libraries;
|
|
5
|
+
this.req = data.req;
|
|
6
|
+
this.res = data.res
|
|
7
|
+
this.assistant = Manager.Assistant({req: data.req, res: data.res}, {accept: 'json'});
|
|
8
|
+
|
|
9
|
+
return this;
|
|
10
|
+
},
|
|
11
|
+
main: async function() {
|
|
12
|
+
let self = this;
|
|
13
|
+
let req = self.req;
|
|
14
|
+
let res = self.res;
|
|
15
|
+
let libraries = self.libraries;
|
|
16
|
+
let assistant = self.assistant;
|
|
17
|
+
|
|
18
|
+
return libraries.cors(req, res, async () => {
|
|
19
|
+
let user = await assistant.authenticate();
|
|
20
|
+
|
|
21
|
+
// Analytics
|
|
22
|
+
let analytics = self.Manager.Analytics({
|
|
23
|
+
assistant: assistant,
|
|
24
|
+
uuid: user.auth.uid,
|
|
25
|
+
})
|
|
26
|
+
.event({
|
|
27
|
+
name: 'authenticate-test',
|
|
28
|
+
params: {},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
assistant.log('Request:', assistant.request.data);
|
|
32
|
+
assistant.log('Result user:', user);
|
|
33
|
+
return res.status(200).json({status: 200, user: user });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = Module;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
let Module = {
|
|
2
|
+
init: async function (Manager, data) {
|
|
3
|
+
this.Manager = Manager;
|
|
4
|
+
this.libraries = Manager.libraries;
|
|
5
|
+
this.req = data.req;
|
|
6
|
+
this.res = data.res
|
|
7
|
+
this.assistant = Manager.Assistant({req: data.req, res: data.res});
|
|
8
|
+
|
|
9
|
+
return this;
|
|
10
|
+
},
|
|
11
|
+
main: async function() {
|
|
12
|
+
let self = this;
|
|
13
|
+
let uuid4;
|
|
14
|
+
let req = self.req;
|
|
15
|
+
let res = self.res;
|
|
16
|
+
let libraries = self.libraries;
|
|
17
|
+
let assistant = self.assistant;
|
|
18
|
+
|
|
19
|
+
return libraries.cors(req, res, async () => {
|
|
20
|
+
let user = await assistant.authenticate();
|
|
21
|
+
|
|
22
|
+
// Analytics
|
|
23
|
+
let analytics = self.Manager.Analytics({
|
|
24
|
+
assistant: assistant,
|
|
25
|
+
uuid: user.auth.uid,
|
|
26
|
+
})
|
|
27
|
+
.event({
|
|
28
|
+
name: 'create-test-accounts',
|
|
29
|
+
params: {},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
let assistant = self.assistant;
|
|
33
|
+
|
|
34
|
+
let response = {
|
|
35
|
+
status: 200,
|
|
36
|
+
data: {}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// await createUser('_test.admin@test.com', '_test.admin', {roles: {admin: true}})
|
|
40
|
+
try {
|
|
41
|
+
response.data = {
|
|
42
|
+
regular: await createUser('_test.admin@test.com', '_test.admin', {roles: {admin: true}}),
|
|
43
|
+
admin: await createUser('_test.regular@test.com', '_test.regular', {roles: {}, password: '123123'}),
|
|
44
|
+
}
|
|
45
|
+
} catch (e) {
|
|
46
|
+
response.status = 500;
|
|
47
|
+
response.error = e;
|
|
48
|
+
assistant.log(e);
|
|
49
|
+
return res.status(response.status).json(response);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if (assistant.isDevelopment()) {
|
|
54
|
+
assistant.log(response);
|
|
55
|
+
return res.status(response.status).json(response);
|
|
56
|
+
} else {
|
|
57
|
+
response.data = {};
|
|
58
|
+
return res.status(response.status).json(response);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function deleteUser(uid) {
|
|
62
|
+
let currentUid;
|
|
63
|
+
return new Promise(async function(resolve, reject) {
|
|
64
|
+
// await libraries.admin.auth().getUserByEmail(email)
|
|
65
|
+
// .then(function(userRecord) {
|
|
66
|
+
// // See the UserRecord reference doc for the contents of userRecord.
|
|
67
|
+
// currentUid = userRecord.toJSON().uid;
|
|
68
|
+
// // assistant.log('Successfully fetched user data:', userRecord.toJSON());
|
|
69
|
+
// })
|
|
70
|
+
// .catch(function(error) {
|
|
71
|
+
// // assistant.log('Error fetching user data:', error);
|
|
72
|
+
// });
|
|
73
|
+
|
|
74
|
+
await libraries.admin.auth().deleteUser(uid)
|
|
75
|
+
.then(function() {
|
|
76
|
+
// assistant.log('Successfully deleted user', currentUid);
|
|
77
|
+
resolve();
|
|
78
|
+
})
|
|
79
|
+
.catch(function(error) {
|
|
80
|
+
// assistant.log('Error deleting user:', currentUid, error);
|
|
81
|
+
resolve();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function createUser(email, uid, options) {
|
|
87
|
+
uuid4 = uuid4 || require('uuid/v4');
|
|
88
|
+
options = options || {};
|
|
89
|
+
options.password = options.password || uuid4();
|
|
90
|
+
options.roles = options.roles || {};
|
|
91
|
+
|
|
92
|
+
let result = {};
|
|
93
|
+
return new Promise(async function(resolve, reject) {
|
|
94
|
+
await deleteUser(uid);
|
|
95
|
+
libraries.admin.auth().createUser({
|
|
96
|
+
uid: uid,
|
|
97
|
+
email: email,
|
|
98
|
+
password: options.password,
|
|
99
|
+
})
|
|
100
|
+
.then(async function(updatedUser) {
|
|
101
|
+
// See the UserRecord reference doc for the contents of userRecord.
|
|
102
|
+
result = {
|
|
103
|
+
uid: uid,
|
|
104
|
+
email: email,
|
|
105
|
+
password: options.password
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
let SignUpHandler = require('../core/actions/sign-up-handler.js');
|
|
109
|
+
SignUpHandler.init(Manager, {
|
|
110
|
+
req: req,
|
|
111
|
+
res: res,
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
SignUpHandler.signUp({
|
|
115
|
+
auth: {
|
|
116
|
+
uid: uid,
|
|
117
|
+
email: email,
|
|
118
|
+
},
|
|
119
|
+
roles: options.roles,
|
|
120
|
+
})
|
|
121
|
+
.then(function(data) {
|
|
122
|
+
assistant.log('Successfully created new user:', uid);
|
|
123
|
+
resolve(result);
|
|
124
|
+
})
|
|
125
|
+
.catch(function(error) {
|
|
126
|
+
assistant.log('Error adding to db:', error);
|
|
127
|
+
reject(error);
|
|
128
|
+
});
|
|
129
|
+
})
|
|
130
|
+
.catch(function(error) {
|
|
131
|
+
assistant.log('Error creating new user:', error);
|
|
132
|
+
reject(error);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
},
|
|
140
|
+
other: async function () {
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
module.exports = Module;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
let Module = {
|
|
2
|
+
init: async function (Manager, data) {
|
|
3
|
+
this.Manager = Manager;
|
|
4
|
+
this.libraries = Manager.libraries;
|
|
5
|
+
this.req = data.req;
|
|
6
|
+
this.res = data.res
|
|
7
|
+
this.assistant = Manager.Assistant({req: data.req, res: data.res});
|
|
8
|
+
|
|
9
|
+
return this;
|
|
10
|
+
},
|
|
11
|
+
main: async function() {
|
|
12
|
+
let self = this;
|
|
13
|
+
let req = self.req;
|
|
14
|
+
let res = self.res;
|
|
15
|
+
let libraries = self.libraries;
|
|
16
|
+
let assistant = self.assistant;
|
|
17
|
+
|
|
18
|
+
return libraries.cors(req, res, async () => {
|
|
19
|
+
let user = await assistant.authenticate();
|
|
20
|
+
|
|
21
|
+
// Analytics
|
|
22
|
+
let analytics = self.Manager.Analytics({
|
|
23
|
+
assistant: assistant,
|
|
24
|
+
uuid: user.auth.uid,
|
|
25
|
+
})
|
|
26
|
+
.event({
|
|
27
|
+
name: 'webhook-test',
|
|
28
|
+
params: {},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
assistant.log(assistant.request);
|
|
32
|
+
return res.status(200).json({status: 200, request: assistant.request.data});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = Module;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
let Module = {
|
|
2
|
+
main: async function () {
|
|
3
|
+
|
|
4
|
+
},
|
|
5
|
+
addToList: async function () {
|
|
6
|
+
return new Promise(function(resolve, reject) {
|
|
7
|
+
// let Mailchimp = require('mailchimp-api-v3');
|
|
8
|
+
// let mailchimp = new Mailchimp(this.ref.functions.config().mailchimp.key);
|
|
9
|
+
// mailchimp.request({
|
|
10
|
+
// method : 'get|post|put|patch|delete',
|
|
11
|
+
// path : 'path for the call, see mailchimp documentation for possible calls',
|
|
12
|
+
// path_params : {
|
|
13
|
+
// //path parameters, see mailchimp documentation for each call
|
|
14
|
+
// },
|
|
15
|
+
// body : {
|
|
16
|
+
// //body parameters, see mailchimp documentation for each call
|
|
17
|
+
// },
|
|
18
|
+
// query : {
|
|
19
|
+
// //query string parameters, see mailchimp documentation for each call
|
|
20
|
+
// }
|
|
21
|
+
// }, callback)
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// const ua = require('universal-analytics');
|
|
2
|
+
const get = require('lodash/get');
|
|
3
|
+
const fetch = require('wonderful-fetch');
|
|
4
|
+
|
|
5
|
+
const uuidRegex = /[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/;
|
|
6
|
+
let uuidv5;
|
|
7
|
+
|
|
8
|
+
const BLACKLISTED_USER_AGENTS = new RegExp([
|
|
9
|
+
/(node-fetch)/,
|
|
10
|
+
].map(r => r.source).join(''));
|
|
11
|
+
|
|
12
|
+
function Analytics(Manager, options) {
|
|
13
|
+
let self = this;
|
|
14
|
+
self.Manager = Manager;
|
|
15
|
+
// self._request = self.Manager._inner || {};
|
|
16
|
+
|
|
17
|
+
// Set id and secret
|
|
18
|
+
// const analyticsId = get(self.Manager, 'config.google_analytics.id', undefined);
|
|
19
|
+
self.analyticsId = self?.Manager?.config?.google_analytics?.id;
|
|
20
|
+
self.analyticsSecret = self?.Manager?.config?.google_analytics?.secret;
|
|
21
|
+
|
|
22
|
+
// Fix options
|
|
23
|
+
options = options || {};
|
|
24
|
+
|
|
25
|
+
// Set properties
|
|
26
|
+
self._assistant = options.assistant || Manager.Assistant();
|
|
27
|
+
self._request = {
|
|
28
|
+
ip: get(self._assistant, 'request.geolocation.ip', '127.0.0.1'),
|
|
29
|
+
country: get(self._assistant, 'request.geolocation.country', ''),
|
|
30
|
+
referrer: get(self._assistant, 'request.referrer', ''),
|
|
31
|
+
userAgent: get(self._assistant, 'request.client.userAgent', ''),
|
|
32
|
+
name: get(self._assistant, 'meta.name', ''),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
self._request.userAgent = self._request.userAgent.match(BLACKLISTED_USER_AGENTS) ? '' : self._request.userAgent;
|
|
36
|
+
|
|
37
|
+
self._ds = 'app';
|
|
38
|
+
self._uuid = options.uuid || self._request.ip || self.Manager.SERVER_UUID;
|
|
39
|
+
self._uuid = self._uuid.match(uuidRegex) ? self._uuid : self.generateId(self._uuid);
|
|
40
|
+
self._debug = typeof options.debug === 'undefined' ? (self.Manager.assistant.isDevelopment()) : options.debug;
|
|
41
|
+
self._pageview = typeof options.pageview === 'undefined' ? true : options.pageview;
|
|
42
|
+
self._version = self.Manager.package.version;
|
|
43
|
+
self._initialized = false;
|
|
44
|
+
|
|
45
|
+
if (!self.analyticsId) {
|
|
46
|
+
console.log('Not initializing because missing analyticsId', self.analyticsId);
|
|
47
|
+
return self;
|
|
48
|
+
} else if (!self.analyticsSecret) {
|
|
49
|
+
console.log('Not initializing because missing analyticsSecret', self.analyticsSecret);
|
|
50
|
+
return self;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// self.user = ua(self.analyticsId, self._uuid, {
|
|
54
|
+
// strictCidFormat: false, // https://analytics.google.com/analytics/web/#/report-home/a104885300w228822596p215709578
|
|
55
|
+
// });
|
|
56
|
+
// self.user.set('ds', 'app');
|
|
57
|
+
|
|
58
|
+
// // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sc
|
|
59
|
+
// if (self._uuid) {
|
|
60
|
+
// self.user.set('uid', self._uuid);
|
|
61
|
+
// }
|
|
62
|
+
// if (self._request.ip) {
|
|
63
|
+
// self.user.set('uip', encodeURIComponent(self._request.ip));
|
|
64
|
+
// }
|
|
65
|
+
// if (self._request.userAgent) {
|
|
66
|
+
// self.user.set('ua', encodeURIComponent(self._request.userAgent));
|
|
67
|
+
// }
|
|
68
|
+
// if (self._request.referrer) {
|
|
69
|
+
// self.user.set('dr', encodeURIComponent(self._request.referrer));
|
|
70
|
+
// }
|
|
71
|
+
|
|
72
|
+
self._initialized = true;
|
|
73
|
+
|
|
74
|
+
if (self._pageview) {
|
|
75
|
+
self.pageview({
|
|
76
|
+
path: self._request.name,
|
|
77
|
+
location: self._request.name,
|
|
78
|
+
host: self._request.name,
|
|
79
|
+
title: self._request.name,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return self;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Analytics.prototype.generateId = function (id) {
|
|
87
|
+
let self = this;
|
|
88
|
+
uuidv5 = uuidv5 || require('uuid').v5;
|
|
89
|
+
let namespace = get(self.Manager, 'config.backend_manager.namespace', undefined);
|
|
90
|
+
|
|
91
|
+
return id && namespace ? uuidv5(id, namespace) : undefined;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
Analytics.prototype.pageview = function (options) {
|
|
95
|
+
let self = this;
|
|
96
|
+
options = options || {};
|
|
97
|
+
options.path = options.path || self._request.name;
|
|
98
|
+
options.location = options.location || self._request.name;
|
|
99
|
+
options.host = options.host || self._request.name;
|
|
100
|
+
options.title = options.title || self._request.name;
|
|
101
|
+
|
|
102
|
+
if (!self._initialized) {
|
|
103
|
+
return self;
|
|
104
|
+
} else if (self._debug) {
|
|
105
|
+
console.log('Skipping Analytics.pageview() because in development', self._uuid, options);
|
|
106
|
+
return self;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// self.user.pageview({
|
|
110
|
+
// dp: options.path,
|
|
111
|
+
// dl: options.location,
|
|
112
|
+
// dh: options.host,
|
|
113
|
+
// dt: options.title,
|
|
114
|
+
// }).send();
|
|
115
|
+
|
|
116
|
+
self.send();
|
|
117
|
+
|
|
118
|
+
return self;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
Analytics.prototype.event = function (options) {
|
|
122
|
+
let self = this;
|
|
123
|
+
options = options || {};
|
|
124
|
+
options.category = options.category;
|
|
125
|
+
options.action = options.action;
|
|
126
|
+
options.label = options.label;
|
|
127
|
+
options.value = options.value;
|
|
128
|
+
options.path = options.path || self._request.name;
|
|
129
|
+
|
|
130
|
+
if (!self._initialized) {
|
|
131
|
+
return self;
|
|
132
|
+
} else if (self._debug) {
|
|
133
|
+
console.log('Skipping Analytics.event() because in development', self._uuid, options);
|
|
134
|
+
return self;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// self.user.event({
|
|
138
|
+
// ec: options.category,
|
|
139
|
+
// ea: options.action,
|
|
140
|
+
// el: options.label,
|
|
141
|
+
// ev: options.value,
|
|
142
|
+
// // dp: options.path || window.location.href,
|
|
143
|
+
// }).send();
|
|
144
|
+
|
|
145
|
+
self.send({
|
|
146
|
+
name: 'tutorial_begin',
|
|
147
|
+
params: {
|
|
148
|
+
campaign_id: 'google_1234',
|
|
149
|
+
campaign: 'Summer_fun',
|
|
150
|
+
source: 'google',
|
|
151
|
+
medium: 'cpc',
|
|
152
|
+
term: 'summer+travel',
|
|
153
|
+
content: 'logolink',
|
|
154
|
+
session_id: '123',
|
|
155
|
+
engagement_time_msec: '100',
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return self;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
Analytics.prototype.send = function (event) {
|
|
163
|
+
let self = this;
|
|
164
|
+
|
|
165
|
+
if (!self._initialized) {
|
|
166
|
+
return self;
|
|
167
|
+
} else if (self._debug) {
|
|
168
|
+
console.log('Skipping Analytics.event() because in development', self._uuid, options);
|
|
169
|
+
return self;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// self.user = ua(self.analyticsId, self._uuid, {
|
|
173
|
+
// strictCidFormat: false, // https://analytics.google.com/analytics/web/#/report-home/a104885300w228822596p215709578
|
|
174
|
+
// });
|
|
175
|
+
// self.user.set('ds', 'app');
|
|
176
|
+
|
|
177
|
+
// // https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sc
|
|
178
|
+
// if (self._uuid) {
|
|
179
|
+
// self.user.set('uid', self._uuid);
|
|
180
|
+
// }
|
|
181
|
+
// if (self._request.ip) {
|
|
182
|
+
// self.user.set('uip', encodeURIComponent(self._request.ip));
|
|
183
|
+
// }
|
|
184
|
+
// if (self._request.userAgent) {
|
|
185
|
+
// self.user.set('ua', encodeURIComponent(self._request.userAgent));
|
|
186
|
+
// }
|
|
187
|
+
// if (self._request.referrer) {
|
|
188
|
+
// self.user.set('dr', encodeURIComponent(self._request.referrer));
|
|
189
|
+
// }
|
|
190
|
+
|
|
191
|
+
/*
|
|
192
|
+
https://stackoverflow.com/questions/68773179/what-should-the-client-id-be-when-sending-events-to-google-analytics-4-using-the
|
|
193
|
+
https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
*/
|
|
197
|
+
|
|
198
|
+
// Send event
|
|
199
|
+
fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${self.analyticsId}&api_secret=${self.analyticsSecret}`, {
|
|
200
|
+
method: 'post',
|
|
201
|
+
body: {
|
|
202
|
+
client_id: self._uuid,
|
|
203
|
+
user_id: self._uuid,
|
|
204
|
+
events: [event],
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
.then((r) => {
|
|
208
|
+
console.log('Analytics.send(): Sent', r);
|
|
209
|
+
})
|
|
210
|
+
.catch((e) => {
|
|
211
|
+
console.error('Analytics.send(): Error sending', e);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return self;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
module.exports = Analytics;
|