@toyz/loom-analytics 0.1.0 → 0.1.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/track.d.ts +11 -2
- package/dist/track.d.ts.map +1 -1
- package/dist/track.js +24 -16
- package/dist/track.js.map +1 -1
- package/package.json +1 -1
package/dist/track.d.ts
CHANGED
|
@@ -4,16 +4,25 @@
|
|
|
4
4
|
* Raw TC39 Stage 3 decorator for event tracking.
|
|
5
5
|
* Dispatches on `context.kind` to support class, method, and accessor targets.
|
|
6
6
|
*
|
|
7
|
+
* - **class**: wraps connectedCallback — fires track() on mount
|
|
7
8
|
* - **method**: wraps the method — fires track() after each invocation
|
|
8
9
|
* - **accessor**: wraps the setter — fires track() on every set
|
|
9
10
|
*
|
|
10
11
|
* ```ts
|
|
11
|
-
*
|
|
12
|
+
* // Static metadata
|
|
13
|
+
* @track("user.save", { section: "profile" })
|
|
12
14
|
* handleSave() { ... }
|
|
13
15
|
*
|
|
16
|
+
* // Dynamic metadata — fn receives the element instance
|
|
17
|
+
* @track("page.view", el => ({ userId: el.userId, route: el.currentRoute }))
|
|
18
|
+
* class Dashboard extends LoomElement { ... }
|
|
19
|
+
*
|
|
14
20
|
* @track("theme.change")
|
|
15
21
|
* accessor theme = "dark";
|
|
16
22
|
* ```
|
|
17
23
|
*/
|
|
18
|
-
|
|
24
|
+
/** Second arg: static object OR a function that receives the element and returns metadata */
|
|
25
|
+
type MetaArg = Record<string, any> | ((el: any) => Record<string, any>);
|
|
26
|
+
export declare function track(event: string, meta?: MetaArg): (value: any, context: DecoratorContext) => any;
|
|
27
|
+
export {};
|
|
19
28
|
//# sourceMappingURL=track.d.ts.map
|
package/dist/track.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"track.d.ts","sourceRoot":"","sources":["../src/track.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"track.d.ts","sourceRoot":"","sources":["../src/track.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAKH,6FAA6F;AAC7F,KAAK,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,GAAG,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;AASxE,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,IACzC,OAAO,GAAG,EAAE,SAAS,gBAAgB,KAAG,GAAG,CAqDpD"}
|
package/dist/track.js
CHANGED
|
@@ -4,60 +4,68 @@
|
|
|
4
4
|
* Raw TC39 Stage 3 decorator for event tracking.
|
|
5
5
|
* Dispatches on `context.kind` to support class, method, and accessor targets.
|
|
6
6
|
*
|
|
7
|
+
* - **class**: wraps connectedCallback — fires track() on mount
|
|
7
8
|
* - **method**: wraps the method — fires track() after each invocation
|
|
8
9
|
* - **accessor**: wraps the setter — fires track() on every set
|
|
9
10
|
*
|
|
10
11
|
* ```ts
|
|
11
|
-
*
|
|
12
|
+
* // Static metadata
|
|
13
|
+
* @track("user.save", { section: "profile" })
|
|
12
14
|
* handleSave() { ... }
|
|
13
15
|
*
|
|
16
|
+
* // Dynamic metadata — fn receives the element instance
|
|
17
|
+
* @track("page.view", el => ({ userId: el.userId, route: el.currentRoute }))
|
|
18
|
+
* class Dashboard extends LoomElement { ... }
|
|
19
|
+
*
|
|
14
20
|
* @track("theme.change")
|
|
15
21
|
* accessor theme = "dark";
|
|
16
22
|
* ```
|
|
17
23
|
*/
|
|
18
24
|
import { app } from "@toyz/loom";
|
|
19
25
|
import { AnalyticsTransport } from "./transport";
|
|
26
|
+
/** Resolve meta: if it's a function, call it with the element; otherwise spread it */
|
|
27
|
+
function resolveMeta(meta, el) {
|
|
28
|
+
if (!meta)
|
|
29
|
+
return {};
|
|
30
|
+
if (typeof meta === "function")
|
|
31
|
+
return meta(el);
|
|
32
|
+
return { ...meta };
|
|
33
|
+
}
|
|
20
34
|
export function track(event, meta) {
|
|
21
35
|
return (value, context) => {
|
|
22
36
|
switch (context.kind) {
|
|
23
37
|
case "class": {
|
|
24
|
-
// Class decorator: stamp an addInitializer that fires on connect
|
|
25
|
-
// Since we can't hook connectedCallback from here, we stamp a marker
|
|
26
|
-
// and let the test/runtime check for it. For simplicity, we fire
|
|
27
|
-
// track at class-definition time for class-level usage (page views).
|
|
28
|
-
// Actually — we can use addInitializer for per-instance setup, but
|
|
29
|
-
// it doesn't have `this` for class decorators in esbuild.
|
|
30
|
-
// Pragmatic solution: wrap the constructor via a returned subclass.
|
|
31
|
-
// BUT esbuild drops class decorator return values.
|
|
32
|
-
// Final approach: directly wrap connectedCallback on the prototype.
|
|
33
38
|
const ctor = value;
|
|
34
39
|
const originalConnected = ctor.prototype.connectedCallback;
|
|
35
40
|
ctor.prototype.connectedCallback = function () {
|
|
36
41
|
if (originalConnected)
|
|
37
42
|
originalConnected.call(this);
|
|
38
43
|
app.get(AnalyticsTransport).track(event, {
|
|
39
|
-
...meta,
|
|
44
|
+
...resolveMeta(meta, this),
|
|
40
45
|
element: this.tagName.toLowerCase(),
|
|
41
46
|
});
|
|
42
47
|
};
|
|
43
48
|
break;
|
|
44
49
|
}
|
|
45
50
|
case "method": {
|
|
46
|
-
// Method decorator: wrap to fire after invocation
|
|
47
51
|
const method = value;
|
|
48
52
|
const key = String(context.name);
|
|
49
53
|
context.addInitializer(function () {
|
|
50
54
|
const original = method;
|
|
55
|
+
const self = this;
|
|
51
56
|
this[key] = function (...args) {
|
|
52
|
-
const result = original.apply(
|
|
53
|
-
app.get(AnalyticsTransport).track(event, {
|
|
57
|
+
const result = original.apply(self, args);
|
|
58
|
+
app.get(AnalyticsTransport).track(event, {
|
|
59
|
+
...resolveMeta(meta, self),
|
|
60
|
+
method: key,
|
|
61
|
+
args,
|
|
62
|
+
});
|
|
54
63
|
return result;
|
|
55
64
|
};
|
|
56
65
|
});
|
|
57
66
|
break;
|
|
58
67
|
}
|
|
59
68
|
case "accessor": {
|
|
60
|
-
// Accessor decorator: wrap the setter to fire on set
|
|
61
69
|
const target = value;
|
|
62
70
|
const key = String(context.name);
|
|
63
71
|
return {
|
|
@@ -67,7 +75,7 @@ export function track(event, meta) {
|
|
|
67
75
|
set(v) {
|
|
68
76
|
target.set.call(this, v);
|
|
69
77
|
app.get(AnalyticsTransport).track(event, {
|
|
70
|
-
...meta,
|
|
78
|
+
...resolveMeta(meta, this),
|
|
71
79
|
property: key,
|
|
72
80
|
value: v,
|
|
73
81
|
});
|
package/dist/track.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"track.js","sourceRoot":"","sources":["../src/track.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"track.js","sourceRoot":"","sources":["../src/track.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAKjD,sFAAsF;AACtF,SAAS,WAAW,CAAC,IAAyB,EAAE,EAAO;IACrD,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IACrB,IAAI,OAAO,IAAI,KAAK,UAAU;QAAE,OAAO,IAAI,CAAC,EAAE,CAAC,CAAC;IAChD,OAAO,EAAE,GAAG,IAAI,EAAE,CAAC;AACrB,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,KAAa,EAAE,IAAc;IACjD,OAAO,CAAC,KAAU,EAAE,OAAyB,EAAO,EAAE;QACpD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;YACrB,KAAK,OAAO,CAAC,CAAC,CAAC;gBACb,MAAM,IAAI,GAAG,KAAiB,CAAC;gBAC/B,MAAM,iBAAiB,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC;gBAC3D,IAAI,CAAC,SAAS,CAAC,iBAAiB,G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