@gravity-ai/api 1.1.7 → 1.1.9
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.d.mts +154 -3
- package/dist/index.d.ts +154 -3
- package/dist/index.js +574 -2
- package/dist/index.mjs +569 -2
- package/dist/opentui.d.mts +1 -1
- package/dist/opentui.d.ts +1 -1
- package/dist/{types-DIfiZ2zL.d.mts → types-C_krF5uC.d.mts} +157 -2
- package/dist/{types-DIfiZ2zL.d.ts → types-C_krF5uC.d.ts} +157 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -8,7 +8,7 @@ var require_package = __commonJS({
|
|
|
8
8
|
"package.json"(exports, module) {
|
|
9
9
|
module.exports = {
|
|
10
10
|
name: "@gravity-ai/api",
|
|
11
|
-
version: "1.1.
|
|
11
|
+
version: "1.1.9",
|
|
12
12
|
description: "Gravity JS SDK for retrieving targeted advertisements",
|
|
13
13
|
main: "dist/index.js",
|
|
14
14
|
module: "dist/index.mjs",
|
|
@@ -137,8 +137,14 @@ async function gravityAds(req, messages, placements, opts = {}) {
|
|
|
137
137
|
...clientIp ? { ip: clientIp } : {}
|
|
138
138
|
}
|
|
139
139
|
};
|
|
140
|
+
if (ctx?.consent && typeof ctx.consent === "object") {
|
|
141
|
+
body.consent = ctx.consent;
|
|
142
|
+
}
|
|
140
143
|
body.relevancy = opts.relevancy ?? 0.2;
|
|
141
144
|
body.testAd = opts.production === true ? false : true;
|
|
145
|
+
const supportsSpec = opts.supportsSpec ?? true;
|
|
146
|
+
body.supportsSpec = supportsSpec;
|
|
147
|
+
if (supportsSpec) body.specCapabilities = opts.specCapabilities ?? "web1";
|
|
142
148
|
if (opts.excludedTopics) body.excludedTopics = opts.excludedTopics;
|
|
143
149
|
const start = performance.now();
|
|
144
150
|
try {
|
|
@@ -213,6 +219,99 @@ var Gravity = class {
|
|
|
213
219
|
}
|
|
214
220
|
};
|
|
215
221
|
|
|
222
|
+
// ../ui/src/consent.ts
|
|
223
|
+
function findCmpApi(name) {
|
|
224
|
+
if (typeof window === "undefined") return void 0;
|
|
225
|
+
let frame = window;
|
|
226
|
+
for (let hops = 0; frame && hops < 10; hops++) {
|
|
227
|
+
try {
|
|
228
|
+
const api = frame[name];
|
|
229
|
+
if (typeof api === "function") return api;
|
|
230
|
+
} catch {
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
if (frame === frame.parent) break;
|
|
234
|
+
frame = frame.parent;
|
|
235
|
+
}
|
|
236
|
+
return void 0;
|
|
237
|
+
}
|
|
238
|
+
var cachedTcf;
|
|
239
|
+
var cachedUsp;
|
|
240
|
+
var tcfListening = false;
|
|
241
|
+
var cmpWaitExhausted = false;
|
|
242
|
+
function ensureListening() {
|
|
243
|
+
if (typeof window === "undefined") return;
|
|
244
|
+
const tcfapi = tcfListening ? void 0 : findCmpApi("__tcfapi");
|
|
245
|
+
if (tcfapi) {
|
|
246
|
+
tcfListening = true;
|
|
247
|
+
try {
|
|
248
|
+
tcfapi("addEventListener", 2, (data, success) => {
|
|
249
|
+
if (!success || typeof data !== "object" || data === null) return;
|
|
250
|
+
const tc = data;
|
|
251
|
+
if (tc.eventStatus === "tcloaded" || tc.eventStatus === "useractioncomplete") {
|
|
252
|
+
cachedTcf = {
|
|
253
|
+
gdprApplies: typeof tc.gdprApplies === "boolean" ? tc.gdprApplies : void 0,
|
|
254
|
+
tcfString: typeof tc.tcString === "string" && tc.tcString ? tc.tcString : void 0
|
|
255
|
+
};
|
|
256
|
+
} else if (typeof tc.gdprApplies === "boolean" && !tc.gdprApplies) {
|
|
257
|
+
cachedTcf = { gdprApplies: false };
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
} catch {
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const uspapi = findCmpApi("__uspapi");
|
|
264
|
+
if (uspapi) {
|
|
265
|
+
try {
|
|
266
|
+
uspapi("getUSPData", 1, (data, success) => {
|
|
267
|
+
if (!success || typeof data !== "object" || data === null) return;
|
|
268
|
+
const usp = data.uspString;
|
|
269
|
+
if (typeof usp === "string" && usp) cachedUsp = usp;
|
|
270
|
+
});
|
|
271
|
+
} catch {
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function detectCmpConsent() {
|
|
276
|
+
ensureListening();
|
|
277
|
+
if (!cachedTcf && !cachedUsp) return void 0;
|
|
278
|
+
const consent = {};
|
|
279
|
+
if (cachedTcf?.gdprApplies !== void 0) consent.gdprApplies = cachedTcf.gdprApplies;
|
|
280
|
+
if (cachedTcf?.tcfString) consent.tcfString = cachedTcf.tcfString;
|
|
281
|
+
if (cachedUsp) consent.usPrivacy = cachedUsp;
|
|
282
|
+
if (Object.keys(consent).length === 0) return void 0;
|
|
283
|
+
return consent;
|
|
284
|
+
}
|
|
285
|
+
var NO_CMP_GRACE_MS = 150;
|
|
286
|
+
var graceStartedAt = typeof window === "undefined" ? 0 : Date.now();
|
|
287
|
+
if (typeof window !== "undefined") ensureListening();
|
|
288
|
+
function readCmpConsent(timeoutMs = 500) {
|
|
289
|
+
ensureListening();
|
|
290
|
+
const immediate = detectCmpConsent();
|
|
291
|
+
if (immediate || cmpWaitExhausted || typeof window === "undefined") {
|
|
292
|
+
return Promise.resolve(immediate);
|
|
293
|
+
}
|
|
294
|
+
return new Promise((resolve) => {
|
|
295
|
+
const started = Date.now();
|
|
296
|
+
const poll = () => {
|
|
297
|
+
const consent = detectCmpConsent();
|
|
298
|
+
if (consent) return resolve(consent);
|
|
299
|
+
const elapsed = Date.now() - started;
|
|
300
|
+
const cmpPresent = Boolean(findCmpApi("__tcfapi") || findCmpApi("__uspapi"));
|
|
301
|
+
if (cmpPresent) {
|
|
302
|
+
if (elapsed >= timeoutMs) {
|
|
303
|
+
cmpWaitExhausted = true;
|
|
304
|
+
return resolve(void 0);
|
|
305
|
+
}
|
|
306
|
+
} else if (elapsed >= timeoutMs || Date.now() - graceStartedAt >= NO_CMP_GRACE_MS) {
|
|
307
|
+
return resolve(void 0);
|
|
308
|
+
}
|
|
309
|
+
setTimeout(poll, 50);
|
|
310
|
+
};
|
|
311
|
+
poll();
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
216
315
|
// context.ts
|
|
217
316
|
function readPixel(method) {
|
|
218
317
|
if (typeof window === "undefined") return void 0;
|
|
@@ -250,12 +349,19 @@ function gravityContext(overrides) {
|
|
|
250
349
|
if (gruid) pixelUserFields.gruid = gruid;
|
|
251
350
|
if (grclid) pixelUserFields.grclid = grclid;
|
|
252
351
|
if (graid) pixelUserFields.graid = graid;
|
|
352
|
+
const consent = overrides.consent ?? (isBrowser ? detectCmpConsent() : void 0);
|
|
253
353
|
return {
|
|
254
354
|
sessionId,
|
|
255
355
|
user: { ...pixelUserFields, ...restUser, id: userId },
|
|
256
|
-
device
|
|
356
|
+
device,
|
|
357
|
+
...consent ? { consent } : {}
|
|
257
358
|
};
|
|
258
359
|
}
|
|
360
|
+
async function gravityContextAsync(overrides) {
|
|
361
|
+
const isBrowser = typeof window !== "undefined" && typeof navigator !== "undefined";
|
|
362
|
+
const consent = overrides.consent ?? (isBrowser ? await readCmpConsent() : void 0);
|
|
363
|
+
return gravityContext(consent ? { ...overrides, consent } : overrides);
|
|
364
|
+
}
|
|
259
365
|
function browserDevice() {
|
|
260
366
|
const nav = typeof navigator !== "undefined" ? navigator : void 0;
|
|
261
367
|
const win = typeof window !== "undefined" ? window : void 0;
|
|
@@ -427,8 +533,11 @@ var Client = class {
|
|
|
427
533
|
*/
|
|
428
534
|
async getAd(params) {
|
|
429
535
|
try {
|
|
536
|
+
const supportsSpec = params.supportsSpec ?? true;
|
|
430
537
|
const body = {
|
|
431
538
|
...params,
|
|
539
|
+
supportsSpec,
|
|
540
|
+
...supportsSpec ? { specCapabilities: params.specCapabilities ?? "web1" } : { specCapabilities: void 0 },
|
|
432
541
|
excludedTopics: params.excludedTopics ?? this.excludedTopics,
|
|
433
542
|
relevancy: params.relevancy ?? this.relevancy
|
|
434
543
|
};
|
|
@@ -514,12 +623,470 @@ async function hashPii(values) {
|
|
|
514
623
|
if (hashedPhone) out.hashed_phone = hashedPhone;
|
|
515
624
|
return out;
|
|
516
625
|
}
|
|
626
|
+
|
|
627
|
+
// gravity-pixel.ts
|
|
628
|
+
function uuid(prefix) {
|
|
629
|
+
const hex = () => Math.random().toString(36).slice(2, 10);
|
|
630
|
+
return prefix + hex() + hex();
|
|
631
|
+
}
|
|
632
|
+
function nowMs() {
|
|
633
|
+
return Date.now();
|
|
634
|
+
}
|
|
635
|
+
var _fs;
|
|
636
|
+
var _path;
|
|
637
|
+
var _os;
|
|
638
|
+
var _modulesLoaded = false;
|
|
639
|
+
async function loadModules() {
|
|
640
|
+
if (_modulesLoaded) return;
|
|
641
|
+
_modulesLoaded = true;
|
|
642
|
+
try {
|
|
643
|
+
_fs = await import("fs");
|
|
644
|
+
_path = await import("path");
|
|
645
|
+
_os = await import("os");
|
|
646
|
+
} catch {
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
function resolveStorageDir(custom) {
|
|
650
|
+
if (custom) return custom;
|
|
651
|
+
const home = _os?.homedir?.() || "/tmp";
|
|
652
|
+
return _path?.join(home, ".gravity") || `${home}/.gravity`;
|
|
653
|
+
}
|
|
654
|
+
function ensureDir(dir) {
|
|
655
|
+
if (!_fs) return;
|
|
656
|
+
try {
|
|
657
|
+
_fs.mkdirSync(dir, { recursive: true });
|
|
658
|
+
} catch {
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function readJSON(filePath, fallback) {
|
|
662
|
+
if (!_fs) return fallback;
|
|
663
|
+
try {
|
|
664
|
+
const raw = _fs.readFileSync(filePath, "utf-8");
|
|
665
|
+
return JSON.parse(raw);
|
|
666
|
+
} catch {
|
|
667
|
+
return fallback;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
function writeJSON(filePath, data) {
|
|
671
|
+
if (!_fs) return;
|
|
672
|
+
try {
|
|
673
|
+
_fs.writeFileSync(filePath, JSON.stringify(data), "utf-8");
|
|
674
|
+
} catch {
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
function collectTerminalContext() {
|
|
678
|
+
const os = _os;
|
|
679
|
+
const runtimeTag = (() => {
|
|
680
|
+
if (typeof globalThis !== "undefined") {
|
|
681
|
+
const g = globalThis;
|
|
682
|
+
if (g.Bun && typeof g.Bun === "object" && "version" in g.Bun) {
|
|
683
|
+
return `Bun/${g.Bun.version}`;
|
|
684
|
+
}
|
|
685
|
+
if (g.Deno && typeof g.Deno === "object") {
|
|
686
|
+
const deno = g.Deno;
|
|
687
|
+
const ver = deno.version;
|
|
688
|
+
return `Deno/${ver?.deno || "unknown"}`;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
692
|
+
return `Node/${process.versions.node}`;
|
|
693
|
+
}
|
|
694
|
+
return "unknown";
|
|
695
|
+
})();
|
|
696
|
+
const cols = typeof process !== "undefined" ? process.stdout?.columns || 80 : 80;
|
|
697
|
+
const rows = typeof process !== "undefined" ? process.stdout?.rows || 24 : 24;
|
|
698
|
+
const env = typeof process !== "undefined" ? process.env : {};
|
|
699
|
+
return {
|
|
700
|
+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
701
|
+
platform: typeof process !== "undefined" ? process.platform : void 0,
|
|
702
|
+
arch: os?.arch(),
|
|
703
|
+
os: os ? `${os.type()} ${os.release()}` : void 0,
|
|
704
|
+
runtime: runtimeTag,
|
|
705
|
+
device_type: env.CI ? "server" : "desktop",
|
|
706
|
+
screen: {
|
|
707
|
+
width: cols,
|
|
708
|
+
height: rows
|
|
709
|
+
},
|
|
710
|
+
viewport: {
|
|
711
|
+
width: cols,
|
|
712
|
+
height: rows
|
|
713
|
+
},
|
|
714
|
+
language: env.LANG?.split(".")[0]?.replace("_", "-") || "en",
|
|
715
|
+
locale: Intl.DateTimeFormat().resolvedOptions().locale,
|
|
716
|
+
hardware_concurrency: os?.cpus()?.length || null,
|
|
717
|
+
device_memory: os ? Math.round(os.totalmem() / (1024 * 1024 * 1024)) : null,
|
|
718
|
+
hostname_hash: null,
|
|
719
|
+
// populated async in start()
|
|
720
|
+
term_program: env.TERM_PROGRAM || void 0,
|
|
721
|
+
term: env.TERM || void 0,
|
|
722
|
+
colorterm: env.COLORTERM || void 0,
|
|
723
|
+
is_ci: !!env.CI
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
async function postJSON(url, payload) {
|
|
727
|
+
if (!url) return;
|
|
728
|
+
try {
|
|
729
|
+
await fetch(url, {
|
|
730
|
+
method: "POST",
|
|
731
|
+
headers: { "Content-Type": "application/json" },
|
|
732
|
+
body: JSON.stringify(payload)
|
|
733
|
+
});
|
|
734
|
+
} catch {
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
var ATTR_PARAMS = [
|
|
738
|
+
"grclid",
|
|
739
|
+
"grcpid",
|
|
740
|
+
"grpuid",
|
|
741
|
+
"gradid",
|
|
742
|
+
"grsig",
|
|
743
|
+
"graid",
|
|
744
|
+
// OpenAI click identifier — used by the OpenAI conversions passthrough.
|
|
745
|
+
"oppref"
|
|
746
|
+
];
|
|
747
|
+
var UTM_PARAMS = [
|
|
748
|
+
"utm_source",
|
|
749
|
+
"utm_medium",
|
|
750
|
+
"utm_campaign",
|
|
751
|
+
"utm_term",
|
|
752
|
+
"utm_content",
|
|
753
|
+
"utm_id",
|
|
754
|
+
"utm_source_platform",
|
|
755
|
+
"utm_creative_format",
|
|
756
|
+
"utm_marketing_tactic",
|
|
757
|
+
"gclid",
|
|
758
|
+
"dclid",
|
|
759
|
+
"gbraid",
|
|
760
|
+
"wbraid",
|
|
761
|
+
"msclkid",
|
|
762
|
+
"fbclid",
|
|
763
|
+
"ttclid",
|
|
764
|
+
"twclid",
|
|
765
|
+
"li_fat_id",
|
|
766
|
+
"rdt_cid",
|
|
767
|
+
"ScCid",
|
|
768
|
+
"epik",
|
|
769
|
+
"ko_click_id"
|
|
770
|
+
];
|
|
771
|
+
var GravityPixel = class {
|
|
772
|
+
constructor(opts) {
|
|
773
|
+
this.attribution = null;
|
|
774
|
+
this.session = null;
|
|
775
|
+
this.isNewSession = false;
|
|
776
|
+
this.contextSent = false;
|
|
777
|
+
this.started = false;
|
|
778
|
+
this.config = {
|
|
779
|
+
pixelId: opts.pixelId,
|
|
780
|
+
eventEndpoint: opts.eventEndpoint || "https://api.trygravity.ai/track/gr-events",
|
|
781
|
+
sessionEndpoint: opts.sessionEndpoint || "https://api.trygravity.ai/track/gr-session",
|
|
782
|
+
sessionTimeoutMinutes: opts.sessionTimeoutMinutes ?? 30,
|
|
783
|
+
attrMaxAgeDays: opts.attrMaxAgeDays ?? 180,
|
|
784
|
+
sessionMaxPages: opts.sessionMaxPages ?? 50,
|
|
785
|
+
hashIdentityFields: opts.hashIdentityFields ?? true,
|
|
786
|
+
storageDir: opts.storageDir || "",
|
|
787
|
+
appName: opts.appName || "terminal"
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
// -----------------------------------------------------------------------
|
|
791
|
+
// Lifecycle
|
|
792
|
+
// -----------------------------------------------------------------------
|
|
793
|
+
/**
|
|
794
|
+
* Initialize the pixel: load persisted attribution + session, register
|
|
795
|
+
* process exit hooks, and fire the initial session update.
|
|
796
|
+
*/
|
|
797
|
+
async start() {
|
|
798
|
+
if (this.started) return;
|
|
799
|
+
this.started = true;
|
|
800
|
+
await loadModules();
|
|
801
|
+
if (!this.config.storageDir) {
|
|
802
|
+
this.config.storageDir = resolveStorageDir();
|
|
803
|
+
}
|
|
804
|
+
ensureDir(this.config.storageDir);
|
|
805
|
+
this.loadAttribution();
|
|
806
|
+
this.loadSession();
|
|
807
|
+
const context = collectTerminalContext();
|
|
808
|
+
if (_os) {
|
|
809
|
+
const hostname = _os.hostname();
|
|
810
|
+
const hashed = await hashPii({ email: hostname });
|
|
811
|
+
context.hostname_hash = hashed.hashed_email || null;
|
|
812
|
+
}
|
|
813
|
+
const onExit = () => {
|
|
814
|
+
this.persistAll();
|
|
815
|
+
};
|
|
816
|
+
process.on("exit", onExit);
|
|
817
|
+
process.on("SIGINT", () => {
|
|
818
|
+
this.shutdownSync();
|
|
819
|
+
process.exit(0);
|
|
820
|
+
});
|
|
821
|
+
process.on("SIGTERM", () => {
|
|
822
|
+
this.shutdownSync();
|
|
823
|
+
process.exit(0);
|
|
824
|
+
});
|
|
825
|
+
await this.sendSessionUpdate(false, null);
|
|
826
|
+
}
|
|
827
|
+
shutdownSync() {
|
|
828
|
+
this.persistAll();
|
|
829
|
+
this.sendSessionUpdate(true, "exit").catch(() => {
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
// -----------------------------------------------------------------------
|
|
833
|
+
// Attribution (replaces captureAttribution + cookie read/write)
|
|
834
|
+
// -----------------------------------------------------------------------
|
|
835
|
+
attrFilePath() {
|
|
836
|
+
return _path?.join(this.config.storageDir, "attribution.json") || `${this.config.storageDir}/attribution.json`;
|
|
837
|
+
}
|
|
838
|
+
loadAttribution() {
|
|
839
|
+
const now = nowMs();
|
|
840
|
+
const stored = readJSON(this.attrFilePath(), {});
|
|
841
|
+
const maxAgeMs = this.config.attrMaxAgeDays * 24 * 60 * 60 * 1e3;
|
|
842
|
+
if (stored.first_seen && now - stored.first_seen > maxAgeMs) {
|
|
843
|
+
this.attribution = {
|
|
844
|
+
gruid: uuid("gruid_"),
|
|
845
|
+
first_seen: now,
|
|
846
|
+
last_seen: now
|
|
847
|
+
};
|
|
848
|
+
this.persistAttribution();
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
this.attribution = {
|
|
852
|
+
gruid: stored.gruid || uuid("gruid_"),
|
|
853
|
+
first_seen: stored.first_seen || now,
|
|
854
|
+
last_seen: now,
|
|
855
|
+
...stored
|
|
856
|
+
};
|
|
857
|
+
this.attribution.last_seen = now;
|
|
858
|
+
this.persistAttribution();
|
|
859
|
+
}
|
|
860
|
+
persistAttribution() {
|
|
861
|
+
if (!this.attribution) return;
|
|
862
|
+
writeJSON(this.attrFilePath(), this.attribution);
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Capture attribution parameters (grclid, graid, UTMs, etc.).
|
|
866
|
+
* Call with params extracted from CLI args, environment variables,
|
|
867
|
+
* deep links, or any other source.
|
|
868
|
+
*
|
|
869
|
+
* Mirrors the same param set that gr-pix.js reads from URL query strings.
|
|
870
|
+
*/
|
|
871
|
+
handleAttribution(params) {
|
|
872
|
+
if (!this.attribution) return;
|
|
873
|
+
const now = nowMs();
|
|
874
|
+
let touched = false;
|
|
875
|
+
for (const key of ATTR_PARAMS) {
|
|
876
|
+
const value = params[key];
|
|
877
|
+
if (value) {
|
|
878
|
+
this.attribution[key] = value;
|
|
879
|
+
touched = true;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
for (const key of UTM_PARAMS) {
|
|
883
|
+
const value = params[key];
|
|
884
|
+
if (value) {
|
|
885
|
+
this.attribution[key] = value;
|
|
886
|
+
touched = true;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
if (touched) {
|
|
890
|
+
this.attribution.last_touch = now;
|
|
891
|
+
}
|
|
892
|
+
this.attribution.last_seen = now;
|
|
893
|
+
this.persistAttribution();
|
|
894
|
+
}
|
|
895
|
+
// -----------------------------------------------------------------------
|
|
896
|
+
// Session (replaces loadSession + persistSession + cookie/localStorage)
|
|
897
|
+
// -----------------------------------------------------------------------
|
|
898
|
+
sessionFilePath() {
|
|
899
|
+
return _path?.join(this.config.storageDir, "session.json") || `${this.config.storageDir}/session.json`;
|
|
900
|
+
}
|
|
901
|
+
loadSession() {
|
|
902
|
+
const now = nowMs();
|
|
903
|
+
const timeoutMs = this.config.sessionTimeoutMinutes * 60 * 1e3;
|
|
904
|
+
const stored = readJSON(this.sessionFilePath(), null);
|
|
905
|
+
const appUri = `terminal://${this.config.appName}`;
|
|
906
|
+
if (!stored || !stored.id || typeof stored.started_at !== "number" || now - (stored.last_seen || 0) > timeoutMs) {
|
|
907
|
+
this.isNewSession = true;
|
|
908
|
+
this.session = {
|
|
909
|
+
id: uuid("gr_sess_"),
|
|
910
|
+
started_at: now,
|
|
911
|
+
last_seen: now,
|
|
912
|
+
page_views: 0,
|
|
913
|
+
landing_url: appUri,
|
|
914
|
+
entry_referrer: null,
|
|
915
|
+
last_page_url: appUri,
|
|
916
|
+
last_page_title: this.config.appName,
|
|
917
|
+
visitor_id: this.attribution?.gruid || "",
|
|
918
|
+
page_history: []
|
|
919
|
+
};
|
|
920
|
+
} else {
|
|
921
|
+
this.isNewSession = false;
|
|
922
|
+
this.session = { ...stored, last_seen: now };
|
|
923
|
+
}
|
|
924
|
+
this.session.page_views += 1;
|
|
925
|
+
this.session.last_page_url = appUri;
|
|
926
|
+
this.session.last_page_title = this.config.appName;
|
|
927
|
+
this.session.visitor_id = this.attribution?.gruid || "";
|
|
928
|
+
this.session.page_history.push({
|
|
929
|
+
url: appUri,
|
|
930
|
+
title: this.config.appName,
|
|
931
|
+
timestamp: new Date(now).toISOString()
|
|
932
|
+
});
|
|
933
|
+
const maxPages = this.config.sessionMaxPages;
|
|
934
|
+
if (this.session.page_history.length > maxPages) {
|
|
935
|
+
this.session.page_history.splice(
|
|
936
|
+
0,
|
|
937
|
+
this.session.page_history.length - maxPages
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
this.persistSession();
|
|
941
|
+
}
|
|
942
|
+
persistSession() {
|
|
943
|
+
if (!this.session) return;
|
|
944
|
+
writeJSON(this.sessionFilePath(), this.session);
|
|
945
|
+
}
|
|
946
|
+
persistAll() {
|
|
947
|
+
this.persistAttribution();
|
|
948
|
+
this.persistSession();
|
|
949
|
+
}
|
|
950
|
+
// -----------------------------------------------------------------------
|
|
951
|
+
// Event tracking (replaces trackEvent + baseEventPayload)
|
|
952
|
+
// -----------------------------------------------------------------------
|
|
953
|
+
/**
|
|
954
|
+
* Fire a tracking event. Mirrors `window.gravityPixel.track()`.
|
|
955
|
+
*
|
|
956
|
+
* @param eventType - e.g. 'query', 'response', 'purchase', 'page_view'
|
|
957
|
+
* @param metadata - arbitrary key-value pairs attached to the event
|
|
958
|
+
*/
|
|
959
|
+
async track(eventType, metadata = {}) {
|
|
960
|
+
if (!this.session || !this.attribution) return;
|
|
961
|
+
const now = nowMs();
|
|
962
|
+
this.session.last_seen = now;
|
|
963
|
+
this.persistSession();
|
|
964
|
+
const payload = {
|
|
965
|
+
event_id: uuid("gr_evt_"),
|
|
966
|
+
event_timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
967
|
+
session_id: this.session.id,
|
|
968
|
+
visitor_id: this.attribution.gruid,
|
|
969
|
+
click_id: this.attribution.grclid || null,
|
|
970
|
+
pixel_id: this.config.pixelId || null,
|
|
971
|
+
page_url: this.session.last_page_url,
|
|
972
|
+
page_title: this.session.last_page_title || null,
|
|
973
|
+
referrer: null,
|
|
974
|
+
user_agent: this.buildUserAgent(),
|
|
975
|
+
language: (typeof process !== "undefined" ? process.env.LANG?.split(".")[0]?.replace("_", "-") : void 0) || "en",
|
|
976
|
+
screen: {
|
|
977
|
+
width: typeof process !== "undefined" ? process.stdout?.columns || 80 : 80,
|
|
978
|
+
height: typeof process !== "undefined" ? process.stdout?.rows || 24 : 24
|
|
979
|
+
},
|
|
980
|
+
event_type: eventType,
|
|
981
|
+
session_start: this.isNewSession,
|
|
982
|
+
metadata: {
|
|
983
|
+
...metadata,
|
|
984
|
+
visitor: {
|
|
985
|
+
id: this.attribution.gruid,
|
|
986
|
+
click_id: this.attribution.grclid || null
|
|
987
|
+
},
|
|
988
|
+
pixel_source: "terminal"
|
|
989
|
+
},
|
|
990
|
+
context: collectTerminalContext(),
|
|
991
|
+
attribution: { ...this.attribution }
|
|
992
|
+
};
|
|
993
|
+
this.isNewSession = false;
|
|
994
|
+
await postJSON(this.config.eventEndpoint, payload);
|
|
995
|
+
}
|
|
996
|
+
/**
|
|
997
|
+
* Track an event with identity fields (email, phone).
|
|
998
|
+
* Fields are hashed with SHA-256 before sending (same normalization
|
|
999
|
+
* as gr-pix.js: email lowercased+trimmed, phone digits-only).
|
|
1000
|
+
*/
|
|
1001
|
+
async trackWithIdentity(eventType, identity, metadata = {}) {
|
|
1002
|
+
if (this.config.hashIdentityFields) {
|
|
1003
|
+
const hashed = await hashPii(identity);
|
|
1004
|
+
metadata.identity = hashed;
|
|
1005
|
+
} else {
|
|
1006
|
+
metadata.identity = identity;
|
|
1007
|
+
}
|
|
1008
|
+
return this.track(eventType, metadata);
|
|
1009
|
+
}
|
|
1010
|
+
// -----------------------------------------------------------------------
|
|
1011
|
+
// Session reporting (replaces sendSessionUpdate + sessionPayload)
|
|
1012
|
+
// -----------------------------------------------------------------------
|
|
1013
|
+
async sendSessionUpdate(ended, reason) {
|
|
1014
|
+
if (!this.session || !this.attribution) return;
|
|
1015
|
+
this.persistSession();
|
|
1016
|
+
const durationMs = Date.now() - this.session.started_at;
|
|
1017
|
+
const payload = {
|
|
1018
|
+
session_id: this.session.id,
|
|
1019
|
+
visitor_id: this.attribution.gruid,
|
|
1020
|
+
pixel_id: this.config.pixelId || null,
|
|
1021
|
+
new_session: this.isNewSession,
|
|
1022
|
+
started_at: new Date(this.session.started_at).toISOString(),
|
|
1023
|
+
last_seen_at: new Date(this.session.last_seen).toISOString(),
|
|
1024
|
+
duration_ms: durationMs,
|
|
1025
|
+
page_views: this.session.page_views,
|
|
1026
|
+
landing_url: this.session.landing_url,
|
|
1027
|
+
last_page_url: this.session.last_page_url,
|
|
1028
|
+
entry_referrer: this.session.entry_referrer,
|
|
1029
|
+
last_referrer: null,
|
|
1030
|
+
ended,
|
|
1031
|
+
reason,
|
|
1032
|
+
attribution: { ...this.attribution },
|
|
1033
|
+
page_history: this.session.page_history
|
|
1034
|
+
};
|
|
1035
|
+
if (!this.contextSent) {
|
|
1036
|
+
payload.context = collectTerminalContext();
|
|
1037
|
+
this.contextSent = true;
|
|
1038
|
+
}
|
|
1039
|
+
await postJSON(this.config.sessionEndpoint, payload);
|
|
1040
|
+
}
|
|
1041
|
+
// -----------------------------------------------------------------------
|
|
1042
|
+
// Public getters (same API surface as window.gravityPixel.*)
|
|
1043
|
+
// -----------------------------------------------------------------------
|
|
1044
|
+
/** Stable first-party visitor ID (gruid_...). */
|
|
1045
|
+
getVisitorId() {
|
|
1046
|
+
return this.attribution?.gruid;
|
|
1047
|
+
}
|
|
1048
|
+
/** Current session ID (gr_sess_...). */
|
|
1049
|
+
getSessionId() {
|
|
1050
|
+
return this.session?.id;
|
|
1051
|
+
}
|
|
1052
|
+
/** Inbound Gravity click ID if present. */
|
|
1053
|
+
getClickId() {
|
|
1054
|
+
return this.attribution?.grclid;
|
|
1055
|
+
}
|
|
1056
|
+
/** Inbound Gravity ad identifier if present. */
|
|
1057
|
+
getGraid() {
|
|
1058
|
+
return this.attribution?.graid;
|
|
1059
|
+
}
|
|
1060
|
+
/** Full attribution object. */
|
|
1061
|
+
getAttribution() {
|
|
1062
|
+
return { ...this.attribution || {} };
|
|
1063
|
+
}
|
|
1064
|
+
// -----------------------------------------------------------------------
|
|
1065
|
+
// Internal helpers
|
|
1066
|
+
// -----------------------------------------------------------------------
|
|
1067
|
+
buildUserAgent() {
|
|
1068
|
+
let runtimeTag = "unknown";
|
|
1069
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
1070
|
+
runtimeTag = `Node/${process.versions.node}`;
|
|
1071
|
+
}
|
|
1072
|
+
const osInfo = _os ? `${_os.type()} ${_os.arch()}` : "unknown";
|
|
1073
|
+
return `gravity-pixel/1.0.0 (${osInfo}; ${runtimeTag})`;
|
|
1074
|
+
}
|
|
1075
|
+
};
|
|
1076
|
+
function gravityPixel(opts) {
|
|
1077
|
+
return new GravityPixel(opts);
|
|
1078
|
+
}
|
|
517
1079
|
export {
|
|
518
1080
|
Client,
|
|
519
1081
|
Gravity,
|
|
1082
|
+
GravityPixel,
|
|
1083
|
+
detectCmpConsent,
|
|
520
1084
|
extractClientIp,
|
|
521
1085
|
gravityAds,
|
|
522
1086
|
gravityContext,
|
|
1087
|
+
gravityContextAsync,
|
|
1088
|
+
gravityPixel,
|
|
523
1089
|
hashPii,
|
|
1090
|
+
readCmpConsent,
|
|
524
1091
|
toGravityMessages
|
|
525
1092
|
};
|
package/dist/opentui.d.mts
CHANGED
package/dist/opentui.d.ts
CHANGED