userlens-analytics-sdk 0.0.1
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/dist/userlens.cjs.js +84 -0
- package/dist/userlens.ejs.js +0 -0
- package/dist/userlens.esm.js +82 -0
- package/package.json +17 -0
- package/rollup.config.js +20 -0
- package/src/EventTracker.js +65 -0
- package/src/api.js +16 -0
- package/src/index.js +3 -0
- package/src/utils.js +3 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const encodeToBase64 = (str) => {
|
|
4
|
+
return btoa(str);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const INGESTOR_URL = "https://events.userlens.io";
|
|
8
|
+
|
|
9
|
+
const track = (teamUuid, body) => {
|
|
10
|
+
const encodedTeamUuid = encodeToBase64(`${teamUuid}:`);
|
|
11
|
+
|
|
12
|
+
return fetch(`${INGESTOR_URL}/event`, {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: {
|
|
15
|
+
"Content-Type": "application/json",
|
|
16
|
+
Authorization: `Basic ${encodedTeamUuid}`,
|
|
17
|
+
},
|
|
18
|
+
body: JSON.stringify(body),
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
class EventTracker {
|
|
23
|
+
constructor(writeCode, userId = "", identifyOnTrack = false) {
|
|
24
|
+
if (!writeCode || !userId) {
|
|
25
|
+
console.error(
|
|
26
|
+
"Userlens EventTracker error: missing writeCode or userId."
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.writeCode = writeCode;
|
|
31
|
+
this.userId = userId;
|
|
32
|
+
this.identifyOnTrack = identifyOnTrack;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
setUserId(userId) {
|
|
36
|
+
this.userId = userId;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setWriteCode(writeCode) {
|
|
40
|
+
this.writeCode = writeCode;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// private method to validate traits object
|
|
44
|
+
#validateTraits(traits) {
|
|
45
|
+
if (!traits || typeof traits !== "object" || Array.isArray(traits)) {
|
|
46
|
+
console.error("Userlens SDK error: Invalid traits object:", traits);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return Object.keys(traits).length > 0; // Return true if traits is not empty
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
identifyUser(traits = {}) {
|
|
54
|
+
if (this.#validateTraits(traits)) {
|
|
55
|
+
track(this.writeCode, {
|
|
56
|
+
type: "identify",
|
|
57
|
+
userId: this.userId,
|
|
58
|
+
traits,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// method to track an event
|
|
64
|
+
trackEvent(eventName = "", traits = {}) {
|
|
65
|
+
if (!eventName) {
|
|
66
|
+
console.error("Userlens trackEvent error: Event name is required");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
track(this.writeCode, {
|
|
71
|
+
type: "track",
|
|
72
|
+
userId: this.userId,
|
|
73
|
+
event: eventName,
|
|
74
|
+
timestamp: new Date().toISOString(),
|
|
75
|
+
source: "userlens-analytics-sdk",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (this.identifyOnTrack) {
|
|
79
|
+
this.identifyUser(traits);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = EventTracker;
|
|
File without changes
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const encodeToBase64 = (str) => {
|
|
2
|
+
return btoa(str);
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
const INGESTOR_URL = "https://events.userlens.io";
|
|
6
|
+
|
|
7
|
+
const track = (teamUuid, body) => {
|
|
8
|
+
const encodedTeamUuid = encodeToBase64(`${teamUuid}:`);
|
|
9
|
+
|
|
10
|
+
return fetch(`${INGESTOR_URL}/event`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: {
|
|
13
|
+
"Content-Type": "application/json",
|
|
14
|
+
Authorization: `Basic ${encodedTeamUuid}`,
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify(body),
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
class EventTracker {
|
|
21
|
+
constructor(writeCode, userId = "", identifyOnTrack = false) {
|
|
22
|
+
if (!writeCode || !userId) {
|
|
23
|
+
console.error(
|
|
24
|
+
"Userlens EventTracker error: missing writeCode or userId."
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.writeCode = writeCode;
|
|
29
|
+
this.userId = userId;
|
|
30
|
+
this.identifyOnTrack = identifyOnTrack;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setUserId(userId) {
|
|
34
|
+
this.userId = userId;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setWriteCode(writeCode) {
|
|
38
|
+
this.writeCode = writeCode;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// private method to validate traits object
|
|
42
|
+
#validateTraits(traits) {
|
|
43
|
+
if (!traits || typeof traits !== "object" || Array.isArray(traits)) {
|
|
44
|
+
console.error("Userlens SDK error: Invalid traits object:", traits);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return Object.keys(traits).length > 0; // Return true if traits is not empty
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
identifyUser(traits = {}) {
|
|
52
|
+
if (this.#validateTraits(traits)) {
|
|
53
|
+
track(this.writeCode, {
|
|
54
|
+
type: "identify",
|
|
55
|
+
userId: this.userId,
|
|
56
|
+
traits,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// method to track an event
|
|
62
|
+
trackEvent(eventName = "", traits = {}) {
|
|
63
|
+
if (!eventName) {
|
|
64
|
+
console.error("Userlens trackEvent error: Event name is required");
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
track(this.writeCode, {
|
|
69
|
+
type: "track",
|
|
70
|
+
userId: this.userId,
|
|
71
|
+
event: eventName,
|
|
72
|
+
timestamp: new Date().toISOString(),
|
|
73
|
+
source: "userlens-analytics-sdk",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
if (this.identifyOnTrack) {
|
|
77
|
+
this.identifyUser(traits);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { EventTracker as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "userlens-analytics-sdk",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "dist/userlens.cjs.js",
|
|
5
|
+
"module": "dist/userlens.esm.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"build": "rollup -c"
|
|
9
|
+
},
|
|
10
|
+
"author": "Wudpecker",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"description": "",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"rollup": "^4.34.6",
|
|
15
|
+
"@rollup/plugin-node-resolve": "^16.0.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// rollup.config.js
|
|
2
|
+
import resolve from "@rollup/plugin-node-resolve";
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
input: "src/index.js", // Entry point for your package
|
|
6
|
+
output: [
|
|
7
|
+
{
|
|
8
|
+
file: "dist/userlens.cjs.js", // CommonJS output for Node.js
|
|
9
|
+
format: "cjs", // Use CommonJS format
|
|
10
|
+
exports: "default",
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
file: "dist/userlens.esm.js", // ESModule output for browsers
|
|
14
|
+
format: "esm", // Use ESModule format
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
plugins: [
|
|
18
|
+
resolve(), // Resolves node modules
|
|
19
|
+
],
|
|
20
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { track } from "./api";
|
|
2
|
+
|
|
3
|
+
class EventTracker {
|
|
4
|
+
constructor(writeCode, userId = "", identifyOnTrack = false) {
|
|
5
|
+
if (!writeCode || !userId) {
|
|
6
|
+
console.error(
|
|
7
|
+
"Userlens EventTracker error: missing writeCode or userId."
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
this.writeCode = writeCode;
|
|
12
|
+
this.userId = userId;
|
|
13
|
+
this.identifyOnTrack = identifyOnTrack;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
setUserId(userId) {
|
|
17
|
+
this.userId = userId;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
setWriteCode(writeCode) {
|
|
21
|
+
this.writeCode = writeCode;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// private method to validate traits object
|
|
25
|
+
#validateTraits(traits) {
|
|
26
|
+
if (!traits || typeof traits !== "object" || Array.isArray(traits)) {
|
|
27
|
+
console.error("Userlens SDK error: Invalid traits object:", traits);
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return Object.keys(traits).length > 0; // Return true if traits is not empty
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
identifyUser(traits = {}) {
|
|
35
|
+
if (this.#validateTraits(traits)) {
|
|
36
|
+
track(this.writeCode, {
|
|
37
|
+
type: "identify",
|
|
38
|
+
userId: this.userId,
|
|
39
|
+
traits,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// method to track an event
|
|
45
|
+
trackEvent(eventName = "", traits = {}) {
|
|
46
|
+
if (!eventName) {
|
|
47
|
+
console.error("Userlens trackEvent error: Event name is required");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
track(this.writeCode, {
|
|
52
|
+
type: "track",
|
|
53
|
+
userId: this.userId,
|
|
54
|
+
event: eventName,
|
|
55
|
+
timestamp: new Date().toISOString(),
|
|
56
|
+
source: "userlens-analytics-sdk",
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (this.identifyOnTrack) {
|
|
60
|
+
this.identifyUser(traits);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default EventTracker;
|
package/src/api.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { encodeToBase64 } from "./utils";
|
|
2
|
+
|
|
3
|
+
const INGESTOR_URL = "https://events.userlens.io";
|
|
4
|
+
|
|
5
|
+
export const track = (teamUuid, body) => {
|
|
6
|
+
const encodedTeamUuid = encodeToBase64(`${teamUuid}:`);
|
|
7
|
+
|
|
8
|
+
return fetch(`${INGESTOR_URL}/event`, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {
|
|
11
|
+
"Content-Type": "application/json",
|
|
12
|
+
Authorization: `Basic ${encodedTeamUuid}`,
|
|
13
|
+
},
|
|
14
|
+
body: JSON.stringify(body),
|
|
15
|
+
});
|
|
16
|
+
};
|
package/src/index.js
ADDED
package/src/utils.js
ADDED