@mpgd/analytics 0.1.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 imjlk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,33 @@
1
+ import type { PlatformTarget } from '@mpgd/platform';
2
+ export type AnalyticsEventName = 'game_started' | 'stage_finished' | 'purchase_started' | 'purchase_completed' | 'purchase_granted' | 'purchase_rejected' | 'rewarded_ad_completed' | 'rewarded_ad_granted' | 'rewarded_ad_rejected' | 'leaderboard_submitted' | 'leaderboard_recorded';
3
+ export type AnalyticsPropertyValue = string | number | boolean;
4
+ export interface AnalyticsEvent {
5
+ readonly name: AnalyticsEventName;
6
+ readonly target: PlatformTarget | 'server';
7
+ readonly sessionId: string;
8
+ readonly occurredAt: string;
9
+ readonly properties: Record<string, AnalyticsPropertyValue>;
10
+ }
11
+ export interface AnalyticsSink {
12
+ track(event: AnalyticsEvent): Promise<void> | void;
13
+ }
14
+ export interface AnalyticsReporter {
15
+ track(input: {
16
+ readonly name: AnalyticsEventName;
17
+ readonly properties?: Record<string, AnalyticsPropertyValue | undefined>;
18
+ readonly occurredAt?: string;
19
+ }): Promise<void>;
20
+ }
21
+ export interface CreateAnalyticsReporterInput {
22
+ readonly target: PlatformTarget | 'server';
23
+ readonly sessionId: string;
24
+ readonly sink?: AnalyticsSink;
25
+ readonly now?: () => string;
26
+ }
27
+ export interface BufferedAnalyticsSink extends AnalyticsSink {
28
+ readonly events: readonly AnalyticsEvent[];
29
+ }
30
+ export declare const assertAnalyticsEvent: (input: unknown) => AnalyticsEvent;
31
+ export declare function createNoopAnalyticsSink(): AnalyticsSink;
32
+ export declare function createBufferedAnalyticsSink(): BufferedAnalyticsSink;
33
+ export declare function createAnalyticsReporter(input: CreateAnalyticsReporterInput): AnalyticsReporter;
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ import * as _a from "typia/lib/internal/_assertGuard";
2
+ import * as _b from "typia/lib/internal/_accessExpressionAsString";
3
+ const __typia_transform__accessExpressionAsString = _b._accessExpressionAsString;
4
+ import typia from 'typia';
5
+ export const assertAnalyticsEvent = (() => {
6
+ const _iv0 = new Set(["game_started", "leaderboard_recorded", "leaderboard_submitted", "purchase_completed", "purchase_granted", "purchase_rejected", "purchase_started", "rewarded_ad_completed", "rewarded_ad_granted", "rewarded_ad_rejected", "stage_finished"]);
7
+ const _av0 = new Set(["game_started", "leaderboard_recorded", "leaderboard_submitted", "purchase_completed", "purchase_granted", "purchase_rejected", "purchase_started", "rewarded_ad_completed", "rewarded_ad_granted", "rewarded_ad_rejected", "stage_finished"]);
8
+ const _ae0 = "(\"game_started\" | \"leaderboard_recorded\" | \"leaderboard_submitted\" | \"purchase_completed\" | \"purchase_granted\" | \"purchase_rejected\" | \"purchase_started\" | \"rewarded_ad_completed\" | \"rewarded_ad_granted\" | \"rewarded_ad_rejected\" | \"stage_finished\")";
9
+ const _ao0 = (input, _path, _exceptionable = true) => (true === _av0.has(input.name) || _a._assertGuard(_exceptionable, {
10
+ method: "typia.createAssert",
11
+ path: _path + ".name",
12
+ expected: _ae0,
13
+ value: input.name
14
+ }, _errorFactory)) && ("ait" === input.target || "android" === input.target || "browser" === input.target || "ios" === input.target || "reddit" === input.target || "server" === input.target || "tauri" === input.target || "telegram" === input.target || _a._assertGuard(_exceptionable, {
15
+ method: "typia.createAssert",
16
+ path: _path + ".target",
17
+ expected: "(\"ait\" | \"android\" | \"browser\" | \"ios\" | \"reddit\" | \"server\" | \"tauri\" | \"telegram\")",
18
+ value: input.target
19
+ }, _errorFactory)) && ("string" === typeof input.sessionId || _a._assertGuard(_exceptionable, {
20
+ method: "typia.createAssert",
21
+ path: _path + ".sessionId",
22
+ expected: "string",
23
+ value: input.sessionId
24
+ }, _errorFactory)) && ("string" === typeof input.occurredAt || _a._assertGuard(_exceptionable, {
25
+ method: "typia.createAssert",
26
+ path: _path + ".occurredAt",
27
+ expected: "string",
28
+ value: input.occurredAt
29
+ }, _errorFactory)) && (("object" === typeof input.properties && null !== input.properties && false === Array.isArray(input.properties) || _a._assertGuard(_exceptionable, {
30
+ method: "typia.createAssert",
31
+ path: _path + ".properties",
32
+ expected: "Record<string, AnalyticsPropertyValue>",
33
+ value: input.properties
34
+ }, _errorFactory)) && _ao1(input.properties, _path + ".properties", true && _exceptionable) || _a._assertGuard(_exceptionable, {
35
+ method: "typia.createAssert",
36
+ path: _path + ".properties",
37
+ expected: "Record<string, AnalyticsPropertyValue>",
38
+ value: input.properties
39
+ }, _errorFactory));
40
+ const _ao1 = (input, _path, _exceptionable = true) => false === _exceptionable || Object.keys(input).every(key => {
41
+ const value = input[key];
42
+ if (undefined === value)
43
+ return true;
44
+ return "string" === typeof value || "number" === typeof value || "boolean" === typeof value || _a._assertGuard(_exceptionable, {
45
+ method: "typia.createAssert",
46
+ path: _path + __typia_transform__accessExpressionAsString(key),
47
+ expected: "(boolean | number | string)",
48
+ value: value
49
+ }, _errorFactory);
50
+ });
51
+ const _io0 = input => true === _iv0.has(input.name) && ("ait" === input.target || "android" === input.target || "browser" === input.target || "ios" === input.target || "reddit" === input.target || "server" === input.target || "tauri" === input.target || "telegram" === input.target) && "string" === typeof input.sessionId && "string" === typeof input.occurredAt && ("object" === typeof input.properties && null !== input.properties && false === Array.isArray(input.properties) && _io1(input.properties));
52
+ const _io1 = input => Object.keys(input).every(key => {
53
+ const value = input[key];
54
+ if (undefined === value)
55
+ return true;
56
+ return "string" === typeof value || "number" === typeof value || "boolean" === typeof value;
57
+ });
58
+ const __is = input => "object" === typeof input && null !== input && _io0(input);
59
+ let _errorFactory;
60
+ return (input, errorFactory) => {
61
+ if (false === __is(input)) {
62
+ _errorFactory = errorFactory;
63
+ ((input, _path, _exceptionable = true) => ("object" === typeof input && null !== input || _a._assertGuard(true, {
64
+ method: "typia.createAssert",
65
+ path: _path + "",
66
+ expected: "AnalyticsEvent",
67
+ value: input
68
+ }, _errorFactory)) && _ao0(input, _path + "", true) || _a._assertGuard(true, {
69
+ method: "typia.createAssert",
70
+ path: _path + "",
71
+ expected: "AnalyticsEvent",
72
+ value: input
73
+ }, _errorFactory))(input, "$input", true);
74
+ }
75
+ return input;
76
+ };
77
+ })();
78
+ export function createNoopAnalyticsSink() {
79
+ return {
80
+ track() { },
81
+ };
82
+ }
83
+ export function createBufferedAnalyticsSink() {
84
+ const events = [];
85
+ return {
86
+ get events() {
87
+ return events;
88
+ },
89
+ track(event) {
90
+ events.push(assertAnalyticsEvent(event));
91
+ },
92
+ };
93
+ }
94
+ export function createAnalyticsReporter(input) {
95
+ const sink = input.sink ?? createNoopAnalyticsSink();
96
+ const now = input.now ?? (() => new Date().toISOString());
97
+ return {
98
+ async track(eventInput) {
99
+ const event = assertAnalyticsEvent({
100
+ name: eventInput.name,
101
+ target: input.target,
102
+ sessionId: input.sessionId,
103
+ occurredAt: eventInput.occurredAt ?? now(),
104
+ properties: compactProperties(eventInput.properties ?? {}),
105
+ });
106
+ try {
107
+ await sink.track(event);
108
+ }
109
+ catch {
110
+ // Analytics is best-effort and must not break gameplay or ledger flows.
111
+ }
112
+ },
113
+ };
114
+ }
115
+ function compactProperties(input) {
116
+ const output = {};
117
+ for (const [key, value] of Object.entries(input)) {
118
+ if (value !== undefined) {
119
+ output[key] = value;
120
+ }
121
+ }
122
+ return output;
123
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@mpgd/analytics",
3
+ "version": "0.1.0",
4
+ "description": "Typed analytics event schema and sink helpers for mpgd platform integrations.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/imjlk/mpgd-kit.git",
9
+ "directory": "packages/analytics"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/imjlk/mpgd-kit/issues"
13
+ },
14
+ "homepage": "https://github.com/imjlk/mpgd-kit#readme",
15
+ "keywords": [
16
+ "mpgd",
17
+ "phaser",
18
+ "vite",
19
+ "typescript",
20
+ "capacitor",
21
+ "apps-in-toss",
22
+ "game-development",
23
+ "game-distribution"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ }
32
+ },
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "dependencies": {
37
+ "typia": "rc",
38
+ "@mpgd/platform": "0.1.0"
39
+ },
40
+ "devDependencies": {
41
+ "ttsc": "0.16.9",
42
+ "typescript": "7.0.1-rc"
43
+ },
44
+ "main": "./dist/index.js",
45
+ "types": "./dist/index.d.ts",
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "check": "ttsc --noEmit",
51
+ "test": "cd ../.. && node tools/run-ttsx.mjs packages/analytics/src/index.test.ts",
52
+ "lint": "ttsc --noEmit",
53
+ "format": "ttsc format",
54
+ "fix": "ttsc fix"
55
+ }
56
+ }