@callforge/tracking-client 0.1.0 → 0.2.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/dist/index.js +21 -17
- package/dist/index.mjs +21 -17
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -175,17 +175,16 @@ var CallForge = class _CallForge {
|
|
|
175
175
|
}
|
|
176
176
|
async fetchSession() {
|
|
177
177
|
const locationId = this.getLocationId();
|
|
178
|
-
if (
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return this.formatSession(cached);
|
|
178
|
+
if (locationId) {
|
|
179
|
+
const cached = this.cache.get(locationId);
|
|
180
|
+
if (cached) {
|
|
181
|
+
return this.formatSession(cached);
|
|
182
|
+
}
|
|
184
183
|
}
|
|
185
184
|
const autoParams = this.getAutoParams();
|
|
186
185
|
const cachedParams = this.cache.getParams();
|
|
187
186
|
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
188
|
-
if (typeof window !== "undefined" && window.__cfTracking) {
|
|
187
|
+
if (locationId && typeof window !== "undefined" && window.__cfTracking) {
|
|
189
188
|
try {
|
|
190
189
|
const data2 = await window.__cfTracking;
|
|
191
190
|
this.saveToCache(locationId, data2, params);
|
|
@@ -193,9 +192,11 @@ var CallForge = class _CallForge {
|
|
|
193
192
|
} catch (e) {
|
|
194
193
|
}
|
|
195
194
|
}
|
|
196
|
-
const sessionId = this.cache.getSessionId(locationId);
|
|
195
|
+
const sessionId = locationId ? this.cache.getSessionId(locationId) : null;
|
|
197
196
|
const data = await this.fetchFromApi(locationId, sessionId, params);
|
|
198
|
-
|
|
197
|
+
if (locationId) {
|
|
198
|
+
this.saveToCache(locationId, data, params);
|
|
199
|
+
}
|
|
199
200
|
return this.formatApiResponse(data);
|
|
200
201
|
}
|
|
201
202
|
getLocationId() {
|
|
@@ -260,9 +261,11 @@ var CallForge = class _CallForge {
|
|
|
260
261
|
buildUrl(locationId, sessionId, params) {
|
|
261
262
|
const { categoryId, endpoint } = this.config;
|
|
262
263
|
const queryParams = {
|
|
263
|
-
categoryId
|
|
264
|
-
loc_physical_ms: locationId
|
|
264
|
+
categoryId
|
|
265
265
|
};
|
|
266
|
+
if (locationId) {
|
|
267
|
+
queryParams.loc_physical_ms = locationId;
|
|
268
|
+
}
|
|
266
269
|
if (sessionId) {
|
|
267
270
|
queryParams.sessionId = sessionId;
|
|
268
271
|
}
|
|
@@ -298,23 +301,24 @@ function getPreloadSnippet(config) {
|
|
|
298
301
|
const script = `(function(){
|
|
299
302
|
var u=new URLSearchParams(location.search);
|
|
300
303
|
var loc=u.get('loc_physical_ms');
|
|
301
|
-
if(!loc)return;
|
|
302
304
|
var ap=['gclid','gbraid','wbraid','msclkid','fbclid'];
|
|
303
305
|
var p={};
|
|
304
306
|
for(var i=0;i<ap.length;i++){var v=u.get(ap[i]);if(v)p[ap[i]]=v}
|
|
305
307
|
var key='${cacheKey}';
|
|
306
|
-
|
|
308
|
+
var sid=null;
|
|
309
|
+
if(loc){try{
|
|
307
310
|
var c=JSON.parse(localStorage.getItem(key));
|
|
308
311
|
if(c&&c.locId===loc&&c.expiresAt>Date.now()+30000){c.params=Object.assign({},c.params,p);window.__cfTracking=Promise.resolve(c);return}
|
|
309
|
-
|
|
312
|
+
sid=(c&&c.locId===loc)?c.sessionId:null;
|
|
310
313
|
var cp=c&&c.params||{};
|
|
311
314
|
p=Object.assign({},p,cp);
|
|
312
|
-
}catch(e){}
|
|
313
|
-
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}
|
|
315
|
+
}catch(e){}}
|
|
316
|
+
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}';
|
|
317
|
+
if(loc)url+='&loc_physical_ms='+loc;
|
|
314
318
|
if(sid)url+='&sessionId='+sid;
|
|
315
319
|
var ks=Object.keys(p).sort();
|
|
316
320
|
for(var j=0;j<ks.length;j++)url+='&'+ks[j]+'='+encodeURIComponent(p[ks[j]]);
|
|
317
|
-
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;try{localStorage.setItem(key,JSON.stringify({locId:loc,sessionId:d.sessionId,phoneNumber:d.phoneNumber,location:d.location,expiresAt:d.expiresAt,params:p}))}catch(e){}return d});
|
|
321
|
+
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;if(loc){try{localStorage.setItem(key,JSON.stringify({locId:loc,sessionId:d.sessionId,phoneNumber:d.phoneNumber,location:d.location,expiresAt:d.expiresAt,params:p}))}catch(e){}}return d});
|
|
318
322
|
})();`.replace(/\n/g, "");
|
|
319
323
|
return `<link rel="preconnect" href="${endpoint}">
|
|
320
324
|
<script>${script}</script>`;
|
package/dist/index.mjs
CHANGED
|
@@ -151,17 +151,16 @@ var CallForge = class _CallForge {
|
|
|
151
151
|
}
|
|
152
152
|
async fetchSession() {
|
|
153
153
|
const locationId = this.getLocationId();
|
|
154
|
-
if (
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return this.formatSession(cached);
|
|
154
|
+
if (locationId) {
|
|
155
|
+
const cached = this.cache.get(locationId);
|
|
156
|
+
if (cached) {
|
|
157
|
+
return this.formatSession(cached);
|
|
158
|
+
}
|
|
160
159
|
}
|
|
161
160
|
const autoParams = this.getAutoParams();
|
|
162
161
|
const cachedParams = this.cache.getParams();
|
|
163
162
|
const params = __spreadValues(__spreadValues(__spreadValues({}, autoParams), cachedParams), this.customParams);
|
|
164
|
-
if (typeof window !== "undefined" && window.__cfTracking) {
|
|
163
|
+
if (locationId && typeof window !== "undefined" && window.__cfTracking) {
|
|
165
164
|
try {
|
|
166
165
|
const data2 = await window.__cfTracking;
|
|
167
166
|
this.saveToCache(locationId, data2, params);
|
|
@@ -169,9 +168,11 @@ var CallForge = class _CallForge {
|
|
|
169
168
|
} catch (e) {
|
|
170
169
|
}
|
|
171
170
|
}
|
|
172
|
-
const sessionId = this.cache.getSessionId(locationId);
|
|
171
|
+
const sessionId = locationId ? this.cache.getSessionId(locationId) : null;
|
|
173
172
|
const data = await this.fetchFromApi(locationId, sessionId, params);
|
|
174
|
-
|
|
173
|
+
if (locationId) {
|
|
174
|
+
this.saveToCache(locationId, data, params);
|
|
175
|
+
}
|
|
175
176
|
return this.formatApiResponse(data);
|
|
176
177
|
}
|
|
177
178
|
getLocationId() {
|
|
@@ -236,9 +237,11 @@ var CallForge = class _CallForge {
|
|
|
236
237
|
buildUrl(locationId, sessionId, params) {
|
|
237
238
|
const { categoryId, endpoint } = this.config;
|
|
238
239
|
const queryParams = {
|
|
239
|
-
categoryId
|
|
240
|
-
loc_physical_ms: locationId
|
|
240
|
+
categoryId
|
|
241
241
|
};
|
|
242
|
+
if (locationId) {
|
|
243
|
+
queryParams.loc_physical_ms = locationId;
|
|
244
|
+
}
|
|
242
245
|
if (sessionId) {
|
|
243
246
|
queryParams.sessionId = sessionId;
|
|
244
247
|
}
|
|
@@ -274,23 +277,24 @@ function getPreloadSnippet(config) {
|
|
|
274
277
|
const script = `(function(){
|
|
275
278
|
var u=new URLSearchParams(location.search);
|
|
276
279
|
var loc=u.get('loc_physical_ms');
|
|
277
|
-
if(!loc)return;
|
|
278
280
|
var ap=['gclid','gbraid','wbraid','msclkid','fbclid'];
|
|
279
281
|
var p={};
|
|
280
282
|
for(var i=0;i<ap.length;i++){var v=u.get(ap[i]);if(v)p[ap[i]]=v}
|
|
281
283
|
var key='${cacheKey}';
|
|
282
|
-
|
|
284
|
+
var sid=null;
|
|
285
|
+
if(loc){try{
|
|
283
286
|
var c=JSON.parse(localStorage.getItem(key));
|
|
284
287
|
if(c&&c.locId===loc&&c.expiresAt>Date.now()+30000){c.params=Object.assign({},c.params,p);window.__cfTracking=Promise.resolve(c);return}
|
|
285
|
-
|
|
288
|
+
sid=(c&&c.locId===loc)?c.sessionId:null;
|
|
286
289
|
var cp=c&&c.params||{};
|
|
287
290
|
p=Object.assign({},p,cp);
|
|
288
|
-
}catch(e){}
|
|
289
|
-
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}
|
|
291
|
+
}catch(e){}}
|
|
292
|
+
var url='${endpoint}/v1/tracking/session?categoryId=${categoryId}';
|
|
293
|
+
if(loc)url+='&loc_physical_ms='+loc;
|
|
290
294
|
if(sid)url+='&sessionId='+sid;
|
|
291
295
|
var ks=Object.keys(p).sort();
|
|
292
296
|
for(var j=0;j<ks.length;j++)url+='&'+ks[j]+'='+encodeURIComponent(p[ks[j]]);
|
|
293
|
-
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;try{localStorage.setItem(key,JSON.stringify({locId:loc,sessionId:d.sessionId,phoneNumber:d.phoneNumber,location:d.location,expiresAt:d.expiresAt,params:p}))}catch(e){}return d});
|
|
297
|
+
window.__cfTracking=fetch(url,{credentials:'omit'}).then(function(r){return r.json()}).then(function(d){d.params=p;if(loc){try{localStorage.setItem(key,JSON.stringify({locId:loc,sessionId:d.sessionId,phoneNumber:d.phoneNumber,location:d.location,expiresAt:d.expiresAt,params:p}))}catch(e){}}return d});
|
|
294
298
|
})();`.replace(/\n/g, "");
|
|
295
299
|
return `<link rel="preconnect" href="${endpoint}">
|
|
296
300
|
<script>${script}</script>`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@callforge/tracking-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,17 +14,17 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"jsdom": "^27.4.0",
|
|
19
|
+
"tsup": "^8.0.0",
|
|
20
|
+
"typescript": "^5.3.0",
|
|
21
|
+
"vitest": "^1.6.0",
|
|
22
|
+
"@callforge/tsconfig": "0.0.0"
|
|
23
|
+
},
|
|
17
24
|
"scripts": {
|
|
18
25
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
19
26
|
"clean": "rm -rf dist",
|
|
20
27
|
"test": "vitest run",
|
|
21
28
|
"test:watch": "vitest"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@callforge/tsconfig": "workspace:*",
|
|
25
|
-
"jsdom": "^27.4.0",
|
|
26
|
-
"tsup": "^8.0.0",
|
|
27
|
-
"typescript": "^5.3.0",
|
|
28
|
-
"vitest": "^1.6.0"
|
|
29
29
|
}
|
|
30
|
-
}
|
|
30
|
+
}
|