trackfox-electron 1.0.0 → 1.0.2
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/index.cjs +235 -0
- package/dist/index.d.cts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +198 -0
- package/dist/preload.cjs +35 -0
- package/dist/preload.d.cts +3 -0
- package/dist/preload.d.ts +3 -0
- package/dist/preload.js +10 -0
- package/package.json +3 -2
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
TrackFox: () => TrackFox
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/detection.ts
|
|
38
|
+
var import_os = __toESM(require("os"), 1);
|
|
39
|
+
function getElectronInfo(app, screen) {
|
|
40
|
+
const primaryDisplay = screen.getPrimaryDisplay();
|
|
41
|
+
return {
|
|
42
|
+
appVersion: app.getVersion(),
|
|
43
|
+
electronVersion: process.versions.electron ?? "unknown",
|
|
44
|
+
nodeVersion: process.versions.node,
|
|
45
|
+
platform: process.platform,
|
|
46
|
+
arch: process.arch,
|
|
47
|
+
osRelease: import_os.default.release(),
|
|
48
|
+
screenResolution: {
|
|
49
|
+
width: primaryDisplay.size.width,
|
|
50
|
+
height: primaryDisplay.size.height
|
|
51
|
+
},
|
|
52
|
+
locale: app.getLocale(),
|
|
53
|
+
isPackaged: app.isPackaged
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/session.ts
|
|
58
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
59
|
+
var import_path = __toESM(require("path"), 1);
|
|
60
|
+
var SESSION_TTL_MS = 30 * 60 * 1e3;
|
|
61
|
+
function generateId(prefix = "") {
|
|
62
|
+
const hex = () => Math.floor(Math.random() * 16).toString(16);
|
|
63
|
+
const uuid = `${prefix}xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, (c) => {
|
|
64
|
+
const r = Math.floor(Math.random() * 16);
|
|
65
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
66
|
+
});
|
|
67
|
+
return uuid;
|
|
68
|
+
}
|
|
69
|
+
function loadOrCreateSession(userDataPath) {
|
|
70
|
+
const storePath = import_path.default.join(userDataPath, "trackfox.json");
|
|
71
|
+
let store = {};
|
|
72
|
+
try {
|
|
73
|
+
if (import_fs.default.existsSync(storePath)) {
|
|
74
|
+
store = JSON.parse(import_fs.default.readFileSync(storePath, "utf-8"));
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
store = {};
|
|
78
|
+
}
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
const visitorId = store.visitorId ?? generateId();
|
|
81
|
+
const sessionExpired = !store.sessionId || !store.sessionExpiry || now > store.sessionExpiry;
|
|
82
|
+
const sessionId = sessionExpired ? generateId("s") : store.sessionId;
|
|
83
|
+
const sessionExpiry = now + SESSION_TTL_MS;
|
|
84
|
+
const updated = { visitorId, sessionId, sessionExpiry };
|
|
85
|
+
try {
|
|
86
|
+
import_fs.default.writeFileSync(storePath, JSON.stringify(updated), "utf-8");
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
return { visitorId, sessionId };
|
|
90
|
+
}
|
|
91
|
+
function refreshSession(userDataPath, sessionId) {
|
|
92
|
+
const storePath = import_path.default.join(userDataPath, "trackfox.json");
|
|
93
|
+
try {
|
|
94
|
+
const raw = import_fs.default.readFileSync(storePath, "utf-8");
|
|
95
|
+
const store = JSON.parse(raw);
|
|
96
|
+
if (store.sessionId === sessionId) {
|
|
97
|
+
store.sessionExpiry = Date.now() + SESSION_TTL_MS;
|
|
98
|
+
import_fs.default.writeFileSync(storePath, JSON.stringify(store), "utf-8");
|
|
99
|
+
}
|
|
100
|
+
} catch {
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/queue.ts
|
|
105
|
+
var EventQueue = class {
|
|
106
|
+
constructor(flushFn) {
|
|
107
|
+
this.queue = [];
|
|
108
|
+
this.online = true;
|
|
109
|
+
this.flush = flushFn;
|
|
110
|
+
}
|
|
111
|
+
setOnline(isOnline) {
|
|
112
|
+
const wasOffline = !this.online;
|
|
113
|
+
this.online = isOnline;
|
|
114
|
+
if (isOnline && wasOffline) {
|
|
115
|
+
this.drainQueue();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async enqueue(payload) {
|
|
119
|
+
if (this.online) {
|
|
120
|
+
try {
|
|
121
|
+
await this.flush(payload);
|
|
122
|
+
} catch (err) {
|
|
123
|
+
console.error("[TrackFox] send failed, queuing for retry:", err);
|
|
124
|
+
this.queue.push(payload);
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
this.queue.push(payload);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async drainQueue() {
|
|
131
|
+
const pending = this.queue.splice(0);
|
|
132
|
+
for (const payload of pending) {
|
|
133
|
+
try {
|
|
134
|
+
await this.flush(payload);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
console.error("[TrackFox] retry failed:", err);
|
|
137
|
+
this.queue.unshift(payload);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
// src/tracker.ts
|
|
145
|
+
var TrackFoxTracker = class {
|
|
146
|
+
constructor() {
|
|
147
|
+
this.options = null;
|
|
148
|
+
this.electronInfo = null;
|
|
149
|
+
this.visitorId = "";
|
|
150
|
+
this.sessionId = "";
|
|
151
|
+
this.userDataPath = "";
|
|
152
|
+
this.queue = null;
|
|
153
|
+
}
|
|
154
|
+
init(opts, app, screen, ipcMain) {
|
|
155
|
+
this.options = {
|
|
156
|
+
apiUrl: "https://trackfox.app",
|
|
157
|
+
debug: false,
|
|
158
|
+
...opts
|
|
159
|
+
};
|
|
160
|
+
this.userDataPath = app.getPath("userData");
|
|
161
|
+
this.electronInfo = getElectronInfo(app, screen);
|
|
162
|
+
const session = loadOrCreateSession(this.userDataPath);
|
|
163
|
+
this.visitorId = session.visitorId;
|
|
164
|
+
this.sessionId = session.sessionId;
|
|
165
|
+
this.queue = new EventQueue((payload) => this.send(payload));
|
|
166
|
+
if (ipcMain) {
|
|
167
|
+
ipcMain.on("trackfox:event", (_e, { eventName, data }) => {
|
|
168
|
+
this.trackEvent(eventName, data);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
trackView(screenName) {
|
|
173
|
+
this.enqueue("appview", screenName);
|
|
174
|
+
}
|
|
175
|
+
trackEvent(eventName, data) {
|
|
176
|
+
this.enqueue("app_event", eventName, { eventName, ...data });
|
|
177
|
+
}
|
|
178
|
+
trackRevenue(data) {
|
|
179
|
+
this.enqueue("app_event", "payment", { eventName: "payment", ...data });
|
|
180
|
+
}
|
|
181
|
+
enqueue(type, href, extraData) {
|
|
182
|
+
if (!this.options || !this.electronInfo || !this.queue) {
|
|
183
|
+
if (this.options?.debug) console.warn("[TrackFox] SDK not initialized \u2014 call TrackFox.init() first");
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
refreshSession(this.userDataPath, this.sessionId);
|
|
187
|
+
const payload = {
|
|
188
|
+
type,
|
|
189
|
+
websiteId: this.options.websiteId,
|
|
190
|
+
domain: this.options.domain,
|
|
191
|
+
href,
|
|
192
|
+
referrer: null,
|
|
193
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
194
|
+
viewport: null,
|
|
195
|
+
visitorId: this.visitorId,
|
|
196
|
+
sessionId: this.sessionId,
|
|
197
|
+
language: this.electronInfo.locale,
|
|
198
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
199
|
+
electronInfo: this.electronInfo,
|
|
200
|
+
extraData
|
|
201
|
+
};
|
|
202
|
+
if (this.options.debug) console.log("[TrackFox]", payload);
|
|
203
|
+
this.queue.enqueue(payload);
|
|
204
|
+
}
|
|
205
|
+
async send(payload) {
|
|
206
|
+
const url = `${this.options.apiUrl}/api/events`;
|
|
207
|
+
let fetchFn;
|
|
208
|
+
try {
|
|
209
|
+
const { net } = await import("electron");
|
|
210
|
+
fetchFn = net.fetch.bind(net);
|
|
211
|
+
} catch {
|
|
212
|
+
fetchFn = fetch;
|
|
213
|
+
}
|
|
214
|
+
const response = await fetchFn(url, {
|
|
215
|
+
method: "POST",
|
|
216
|
+
headers: {
|
|
217
|
+
"Content-Type": "application/json",
|
|
218
|
+
"accept": "application/json",
|
|
219
|
+
"accept-language": "en-US,en;q=0.9",
|
|
220
|
+
"accept-encoding": "gzip, deflate, br"
|
|
221
|
+
},
|
|
222
|
+
body: JSON.stringify(payload)
|
|
223
|
+
});
|
|
224
|
+
if (!response.ok) {
|
|
225
|
+
const body = await response.text().catch(() => "");
|
|
226
|
+
throw new Error(`TrackFox: HTTP ${response.status}${body ? ` \u2014 ${body}` : ""}`);
|
|
227
|
+
}
|
|
228
|
+
if (this.options?.debug) console.log("[TrackFox] event sent:", payload.type, payload.href);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
var TrackFox = new TrackFoxTracker();
|
|
232
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
233
|
+
0 && (module.exports = {
|
|
234
|
+
TrackFox
|
|
235
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { App, Screen, IpcMain } from 'electron';
|
|
2
|
+
|
|
3
|
+
interface TrackFoxOptions {
|
|
4
|
+
websiteId: string;
|
|
5
|
+
domain: string;
|
|
6
|
+
apiUrl?: string;
|
|
7
|
+
debug?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface ElectronInfo {
|
|
10
|
+
appVersion: string;
|
|
11
|
+
electronVersion: string;
|
|
12
|
+
nodeVersion: string;
|
|
13
|
+
platform: string;
|
|
14
|
+
arch: string;
|
|
15
|
+
osRelease: string;
|
|
16
|
+
screenResolution?: {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
};
|
|
20
|
+
locale: string;
|
|
21
|
+
isPackaged: boolean;
|
|
22
|
+
}
|
|
23
|
+
interface RevenueData {
|
|
24
|
+
email: string;
|
|
25
|
+
amnt: number;
|
|
26
|
+
currency?: string;
|
|
27
|
+
orderId?: string;
|
|
28
|
+
productId?: string;
|
|
29
|
+
category?: string;
|
|
30
|
+
}
|
|
31
|
+
interface EventPayload {
|
|
32
|
+
type: string;
|
|
33
|
+
websiteId: string;
|
|
34
|
+
domain: string;
|
|
35
|
+
href: string;
|
|
36
|
+
referrer: null;
|
|
37
|
+
timestamp: string;
|
|
38
|
+
viewport: null;
|
|
39
|
+
visitorId: string;
|
|
40
|
+
sessionId: string;
|
|
41
|
+
language: string;
|
|
42
|
+
timezone: string;
|
|
43
|
+
electronInfo: ElectronInfo;
|
|
44
|
+
extraData?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class TrackFoxTracker {
|
|
48
|
+
private options;
|
|
49
|
+
private electronInfo;
|
|
50
|
+
private visitorId;
|
|
51
|
+
private sessionId;
|
|
52
|
+
private userDataPath;
|
|
53
|
+
private queue;
|
|
54
|
+
init(opts: TrackFoxOptions, app: App, screen: Screen, ipcMain?: IpcMain): void;
|
|
55
|
+
trackView(screenName: string): void;
|
|
56
|
+
trackEvent(eventName: string, data?: Record<string, unknown>): void;
|
|
57
|
+
trackRevenue(data: RevenueData): void;
|
|
58
|
+
private enqueue;
|
|
59
|
+
private send;
|
|
60
|
+
}
|
|
61
|
+
declare const TrackFox: TrackFoxTracker;
|
|
62
|
+
|
|
63
|
+
export { type ElectronInfo, type EventPayload, type RevenueData, TrackFox, type TrackFoxOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { App, Screen, IpcMain } from 'electron';
|
|
2
|
+
|
|
3
|
+
interface TrackFoxOptions {
|
|
4
|
+
websiteId: string;
|
|
5
|
+
domain: string;
|
|
6
|
+
apiUrl?: string;
|
|
7
|
+
debug?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface ElectronInfo {
|
|
10
|
+
appVersion: string;
|
|
11
|
+
electronVersion: string;
|
|
12
|
+
nodeVersion: string;
|
|
13
|
+
platform: string;
|
|
14
|
+
arch: string;
|
|
15
|
+
osRelease: string;
|
|
16
|
+
screenResolution?: {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
};
|
|
20
|
+
locale: string;
|
|
21
|
+
isPackaged: boolean;
|
|
22
|
+
}
|
|
23
|
+
interface RevenueData {
|
|
24
|
+
email: string;
|
|
25
|
+
amnt: number;
|
|
26
|
+
currency?: string;
|
|
27
|
+
orderId?: string;
|
|
28
|
+
productId?: string;
|
|
29
|
+
category?: string;
|
|
30
|
+
}
|
|
31
|
+
interface EventPayload {
|
|
32
|
+
type: string;
|
|
33
|
+
websiteId: string;
|
|
34
|
+
domain: string;
|
|
35
|
+
href: string;
|
|
36
|
+
referrer: null;
|
|
37
|
+
timestamp: string;
|
|
38
|
+
viewport: null;
|
|
39
|
+
visitorId: string;
|
|
40
|
+
sessionId: string;
|
|
41
|
+
language: string;
|
|
42
|
+
timezone: string;
|
|
43
|
+
electronInfo: ElectronInfo;
|
|
44
|
+
extraData?: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare class TrackFoxTracker {
|
|
48
|
+
private options;
|
|
49
|
+
private electronInfo;
|
|
50
|
+
private visitorId;
|
|
51
|
+
private sessionId;
|
|
52
|
+
private userDataPath;
|
|
53
|
+
private queue;
|
|
54
|
+
init(opts: TrackFoxOptions, app: App, screen: Screen, ipcMain?: IpcMain): void;
|
|
55
|
+
trackView(screenName: string): void;
|
|
56
|
+
trackEvent(eventName: string, data?: Record<string, unknown>): void;
|
|
57
|
+
trackRevenue(data: RevenueData): void;
|
|
58
|
+
private enqueue;
|
|
59
|
+
private send;
|
|
60
|
+
}
|
|
61
|
+
declare const TrackFox: TrackFoxTracker;
|
|
62
|
+
|
|
63
|
+
export { type ElectronInfo, type EventPayload, type RevenueData, TrackFox, type TrackFoxOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// src/detection.ts
|
|
2
|
+
import os from "os";
|
|
3
|
+
function getElectronInfo(app, screen) {
|
|
4
|
+
const primaryDisplay = screen.getPrimaryDisplay();
|
|
5
|
+
return {
|
|
6
|
+
appVersion: app.getVersion(),
|
|
7
|
+
electronVersion: process.versions.electron ?? "unknown",
|
|
8
|
+
nodeVersion: process.versions.node,
|
|
9
|
+
platform: process.platform,
|
|
10
|
+
arch: process.arch,
|
|
11
|
+
osRelease: os.release(),
|
|
12
|
+
screenResolution: {
|
|
13
|
+
width: primaryDisplay.size.width,
|
|
14
|
+
height: primaryDisplay.size.height
|
|
15
|
+
},
|
|
16
|
+
locale: app.getLocale(),
|
|
17
|
+
isPackaged: app.isPackaged
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/session.ts
|
|
22
|
+
import fs from "fs";
|
|
23
|
+
import path from "path";
|
|
24
|
+
var SESSION_TTL_MS = 30 * 60 * 1e3;
|
|
25
|
+
function generateId(prefix = "") {
|
|
26
|
+
const hex = () => Math.floor(Math.random() * 16).toString(16);
|
|
27
|
+
const uuid = `${prefix}xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`.replace(/[xy]/g, (c) => {
|
|
28
|
+
const r = Math.floor(Math.random() * 16);
|
|
29
|
+
return (c === "x" ? r : r & 3 | 8).toString(16);
|
|
30
|
+
});
|
|
31
|
+
return uuid;
|
|
32
|
+
}
|
|
33
|
+
function loadOrCreateSession(userDataPath) {
|
|
34
|
+
const storePath = path.join(userDataPath, "trackfox.json");
|
|
35
|
+
let store = {};
|
|
36
|
+
try {
|
|
37
|
+
if (fs.existsSync(storePath)) {
|
|
38
|
+
store = JSON.parse(fs.readFileSync(storePath, "utf-8"));
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
store = {};
|
|
42
|
+
}
|
|
43
|
+
const now = Date.now();
|
|
44
|
+
const visitorId = store.visitorId ?? generateId();
|
|
45
|
+
const sessionExpired = !store.sessionId || !store.sessionExpiry || now > store.sessionExpiry;
|
|
46
|
+
const sessionId = sessionExpired ? generateId("s") : store.sessionId;
|
|
47
|
+
const sessionExpiry = now + SESSION_TTL_MS;
|
|
48
|
+
const updated = { visitorId, sessionId, sessionExpiry };
|
|
49
|
+
try {
|
|
50
|
+
fs.writeFileSync(storePath, JSON.stringify(updated), "utf-8");
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
return { visitorId, sessionId };
|
|
54
|
+
}
|
|
55
|
+
function refreshSession(userDataPath, sessionId) {
|
|
56
|
+
const storePath = path.join(userDataPath, "trackfox.json");
|
|
57
|
+
try {
|
|
58
|
+
const raw = fs.readFileSync(storePath, "utf-8");
|
|
59
|
+
const store = JSON.parse(raw);
|
|
60
|
+
if (store.sessionId === sessionId) {
|
|
61
|
+
store.sessionExpiry = Date.now() + SESSION_TTL_MS;
|
|
62
|
+
fs.writeFileSync(storePath, JSON.stringify(store), "utf-8");
|
|
63
|
+
}
|
|
64
|
+
} catch {
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/queue.ts
|
|
69
|
+
var EventQueue = class {
|
|
70
|
+
constructor(flushFn) {
|
|
71
|
+
this.queue = [];
|
|
72
|
+
this.online = true;
|
|
73
|
+
this.flush = flushFn;
|
|
74
|
+
}
|
|
75
|
+
setOnline(isOnline) {
|
|
76
|
+
const wasOffline = !this.online;
|
|
77
|
+
this.online = isOnline;
|
|
78
|
+
if (isOnline && wasOffline) {
|
|
79
|
+
this.drainQueue();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async enqueue(payload) {
|
|
83
|
+
if (this.online) {
|
|
84
|
+
try {
|
|
85
|
+
await this.flush(payload);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.error("[TrackFox] send failed, queuing for retry:", err);
|
|
88
|
+
this.queue.push(payload);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
this.queue.push(payload);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async drainQueue() {
|
|
95
|
+
const pending = this.queue.splice(0);
|
|
96
|
+
for (const payload of pending) {
|
|
97
|
+
try {
|
|
98
|
+
await this.flush(payload);
|
|
99
|
+
} catch (err) {
|
|
100
|
+
console.error("[TrackFox] retry failed:", err);
|
|
101
|
+
this.queue.unshift(payload);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// src/tracker.ts
|
|
109
|
+
var TrackFoxTracker = class {
|
|
110
|
+
constructor() {
|
|
111
|
+
this.options = null;
|
|
112
|
+
this.electronInfo = null;
|
|
113
|
+
this.visitorId = "";
|
|
114
|
+
this.sessionId = "";
|
|
115
|
+
this.userDataPath = "";
|
|
116
|
+
this.queue = null;
|
|
117
|
+
}
|
|
118
|
+
init(opts, app, screen, ipcMain) {
|
|
119
|
+
this.options = {
|
|
120
|
+
apiUrl: "https://trackfox.app",
|
|
121
|
+
debug: false,
|
|
122
|
+
...opts
|
|
123
|
+
};
|
|
124
|
+
this.userDataPath = app.getPath("userData");
|
|
125
|
+
this.electronInfo = getElectronInfo(app, screen);
|
|
126
|
+
const session = loadOrCreateSession(this.userDataPath);
|
|
127
|
+
this.visitorId = session.visitorId;
|
|
128
|
+
this.sessionId = session.sessionId;
|
|
129
|
+
this.queue = new EventQueue((payload) => this.send(payload));
|
|
130
|
+
if (ipcMain) {
|
|
131
|
+
ipcMain.on("trackfox:event", (_e, { eventName, data }) => {
|
|
132
|
+
this.trackEvent(eventName, data);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
trackView(screenName) {
|
|
137
|
+
this.enqueue("appview", screenName);
|
|
138
|
+
}
|
|
139
|
+
trackEvent(eventName, data) {
|
|
140
|
+
this.enqueue("app_event", eventName, { eventName, ...data });
|
|
141
|
+
}
|
|
142
|
+
trackRevenue(data) {
|
|
143
|
+
this.enqueue("app_event", "payment", { eventName: "payment", ...data });
|
|
144
|
+
}
|
|
145
|
+
enqueue(type, href, extraData) {
|
|
146
|
+
if (!this.options || !this.electronInfo || !this.queue) {
|
|
147
|
+
if (this.options?.debug) console.warn("[TrackFox] SDK not initialized \u2014 call TrackFox.init() first");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
refreshSession(this.userDataPath, this.sessionId);
|
|
151
|
+
const payload = {
|
|
152
|
+
type,
|
|
153
|
+
websiteId: this.options.websiteId,
|
|
154
|
+
domain: this.options.domain,
|
|
155
|
+
href,
|
|
156
|
+
referrer: null,
|
|
157
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
158
|
+
viewport: null,
|
|
159
|
+
visitorId: this.visitorId,
|
|
160
|
+
sessionId: this.sessionId,
|
|
161
|
+
language: this.electronInfo.locale,
|
|
162
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
163
|
+
electronInfo: this.electronInfo,
|
|
164
|
+
extraData
|
|
165
|
+
};
|
|
166
|
+
if (this.options.debug) console.log("[TrackFox]", payload);
|
|
167
|
+
this.queue.enqueue(payload);
|
|
168
|
+
}
|
|
169
|
+
async send(payload) {
|
|
170
|
+
const url = `${this.options.apiUrl}/api/events`;
|
|
171
|
+
let fetchFn;
|
|
172
|
+
try {
|
|
173
|
+
const { net } = await import("electron");
|
|
174
|
+
fetchFn = net.fetch.bind(net);
|
|
175
|
+
} catch {
|
|
176
|
+
fetchFn = fetch;
|
|
177
|
+
}
|
|
178
|
+
const response = await fetchFn(url, {
|
|
179
|
+
method: "POST",
|
|
180
|
+
headers: {
|
|
181
|
+
"Content-Type": "application/json",
|
|
182
|
+
"accept": "application/json",
|
|
183
|
+
"accept-language": "en-US,en;q=0.9",
|
|
184
|
+
"accept-encoding": "gzip, deflate, br"
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify(payload)
|
|
187
|
+
});
|
|
188
|
+
if (!response.ok) {
|
|
189
|
+
const body = await response.text().catch(() => "");
|
|
190
|
+
throw new Error(`TrackFox: HTTP ${response.status}${body ? ` \u2014 ${body}` : ""}`);
|
|
191
|
+
}
|
|
192
|
+
if (this.options?.debug) console.log("[TrackFox] event sent:", payload.type, payload.href);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
var TrackFox = new TrackFoxTracker();
|
|
196
|
+
export {
|
|
197
|
+
TrackFox
|
|
198
|
+
};
|
package/dist/preload.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/preload.ts
|
|
21
|
+
var preload_exports = {};
|
|
22
|
+
__export(preload_exports, {
|
|
23
|
+
exposeToRenderer: () => exposeToRenderer
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(preload_exports);
|
|
26
|
+
var import_electron = require("electron");
|
|
27
|
+
function exposeToRenderer() {
|
|
28
|
+
import_electron.contextBridge.exposeInMainWorld("trackfox", (eventName, data) => {
|
|
29
|
+
import_electron.ipcRenderer.send("trackfox:event", { eventName, data });
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
+
0 && (module.exports = {
|
|
34
|
+
exposeToRenderer
|
|
35
|
+
});
|
package/dist/preload.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/preload.ts
|
|
2
|
+
import { contextBridge, ipcRenderer } from "electron";
|
|
3
|
+
function exposeToRenderer() {
|
|
4
|
+
contextBridge.exposeInMainWorld("trackfox", (eventName, data) => {
|
|
5
|
+
ipcRenderer.send("trackfox:event", { eventName, data });
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
exposeToRenderer
|
|
10
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trackfox-electron",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "TrackFox Analytics SDK for Electron desktop apps",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
],
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "tsup",
|
|
28
|
-
"dev": "tsup --watch"
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"prepublishOnly": "tsup"
|
|
29
30
|
},
|
|
30
31
|
"keywords": [
|
|
31
32
|
"trackfox",
|