Skip to content

Commit 53998e5

Browse files
authored
[Fix] tracking errors async (#483)
1 parent 3b06efa commit 53998e5

File tree

7 files changed

+69
-99
lines changed

7 files changed

+69
-99
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 23.12.6
2+
- Mitigated an issue where error tracking could prevent SDK initialization in async mode
3+
14
## 23.12.5
25
- Mitigated an issue where the SDK was not emptying the async queue explicity when closing a browser
36

cypress/integration/async_queue.js

+3-42
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ describe("Test Countly.q related methods and processes", () => {
4141
expect(Countly.q.length).to.equal(0);
4242

4343
// Add 4 events to the .q
44+
Countly.q.push(["track_errors"]); // adding this as calling it during init used to cause an error (at v23.12.5)
4445
Countly.q.push(["add_event", event(1)]);
4546
Countly.q.push(["add_event", event(2)]);
4647
Countly.q.push(["add_event", event(3)]);
4748
Countly.q.push(["add_event", event(4)]);
4849
// Check that the .q has 4 events
49-
expect(Countly.q.length).to.equal(4);
50+
expect(Countly.q.length).to.equal(5);
5051

5152
cy.fetch_local_event_queue().then((rq) => {
5253
// Check that events are still in .q
53-
expect(Countly.q.length).to.equal(4);
54+
expect(Countly.q.length).to.equal(5);
5455

5556
// Check that the event queue is empty
5657
expect(rq.length).to.equal(0);
@@ -139,46 +140,6 @@ describe("Test Countly.q related methods and processes", () => {
139140
});
140141
});
141142
});
142-
// This test checks if clear_stored_id set to true during init we call processAsyncQueue (it sends events from .q to event queue and then to request queue)
143-
it("Check clear_stored_id set to true empties the .q", () => {
144-
hp.haltAndClearStorage(() => {
145-
// Disable heartbeat
146-
Countly.noHeartBeat = true;
147-
Countly.q = [];
148-
localStorage.setItem("YOUR_APP_KEY/cly_id", "old_user_id"); // Set old device ID for clear_stored_id to work
149-
150-
// Add 4 events to the .q
151-
Countly.q.push(["add_event", event(1)]);
152-
Countly.q.push(["add_event", event(2)]);
153-
Countly.q.push(["add_event", event(3)]);
154-
Countly.q.push(["add_event", event(4)]);
155-
156-
// Check that the .q has 4 events
157-
expect(Countly.q.length).to.equal(4);
158-
159-
// Init the SDK with clear_stored_id set to true
160-
initMain(true);
161-
cy.wait(1000);
162-
163-
// Check that the .q is empty
164-
expect(Countly.q.length).to.equal(0);
165-
166-
cy.fetch_local_event_queue().then((rq) => {
167-
// Check that the event queue is empty because processAsyncQueue sends events from .q to event queue and then to request queue
168-
expect(rq.length).to.equal(0);
169-
170-
cy.fetch_local_request_queue().then((rq_2) => {
171-
// Check that events are now in request queue
172-
expect(rq_2.length).to.equal(1);
173-
const eventsArray = JSON.parse(rq_2[0].events);
174-
expect(eventsArray[0].key).to.equal("event_1");
175-
expect(eventsArray[1].key).to.equal("event_2");
176-
expect(eventsArray[2].key).to.equal("event_3");
177-
expect(eventsArray[3].key).to.equal("event_4");
178-
});
179-
});
180-
});
181-
});
182143
// This test checks if calling user_details triggers processAsyncQueue (it sends events from .q to event queue and then to request queue)
183144
it("Check sending user details empties .q", () => {
184145
hp.haltAndClearStorage(() => {

cypress/integration/bridge_utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function initMain(name, version) {
1616
}
1717

1818
const SDK_NAME = "javascript_native_web";
19-
const SDK_VERSION = "23.12.5";
19+
const SDK_VERSION = "23.12.6";
2020

2121
// tests
2222
describe("Bridged SDK Utilities Tests", () => {

cypress/integration/utm.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("UTM tests ", () => {
1919
it("Checks if a single default utm tag works", () => {
2020
hp.haltAndClearStorage(() => {
2121
initMulti("YOUR_APP_KEY", "?utm_source=hehe", undefined);
22+
Countly.q.push(["track_errors"]); // adding this as calling it during init used to cause an error (at v23.12.5)
2223
cy.fetch_local_request_queue().then((rq) => {
2324
cy.log(rq);
2425
const custom = JSON.parse(rq[0].user_details).custom;

lib/countly.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
statusCode: "cly_hc_status_code",
197197
errorMessage: "cly_hc_error_message"
198198
});
199-
var SDK_VERSION = "23.12.5";
199+
var SDK_VERSION = "23.12.6";
200200
var SDK_NAME = "javascript_native_web";
201201

202202
// Using this on document.referrer would return an array with 17 elements in it. The 12th element (array[11]) would be the path we are looking for. Others would be things like password and such (use https://regex101.com/ to check more)
@@ -956,8 +956,7 @@
956956
log(logLevelEnums.DEBUG, "initialize, No device ID type info from the previous session, falling back to DEVELOPER_SUPPLIED, for event flushing");
957957
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
958958
}
959-
// process async queue before sending events
960-
processAsyncQueue();
959+
// don't process async queue here, just send the events (most likely async data is for the new user)
961960
sendEventsForced();
962961
// set them back to their initial values
963962
this.device_id = undefined;
@@ -1148,7 +1147,6 @@
11481147
if (sessionCookieTimeout !== configurationDefaultValues.SESSION_COOKIE_TIMEOUT) {
11491148
log(logLevelEnums.DEBUG, "initialize, session_cookie_timeout set to:[" + sessionCookieTimeout + "] minutes to expire a cookies session");
11501149
}
1151-
log(logLevelEnums.INFO, "initialize, Countly initialized");
11521150
var deviceIdParamValue = null;
11531151
var searchQuery = self.getSearchQuery();
11541152
var hasUTM = false;
@@ -1274,6 +1272,7 @@
12741272
}
12751273
// send instant health check request
12761274
HealthCheck.sendInstantHCRequest();
1275+
log(logLevelEnums.INFO, "initialize, Countly initialized");
12771276
};
12781277

12791278
/**
@@ -4392,6 +4391,10 @@
43924391
* @memberof Countly._internals
43934392
*/
43944393
function processAsyncQueue() {
4394+
if (typeof Countly === "undefined" || typeof Countly.i === "undefined") {
4395+
log(logLevelEnums.DEBUG, "Countly is not finished initialization yet, will process the queue after initialization is done");
4396+
return;
4397+
}
43954398
var q = Countly.q;
43964399
Countly.q = [];
43974400
for (var i = 0; i < q.length; i++) {
@@ -4411,6 +4414,8 @@
44114414
} catch (error) {
44124415
// possibly first init and no other instance
44134416
log(logLevelEnums.DEBUG, "No instance found for the provided key while processing async queue");
4417+
Countly.q.push(req); // return it back to queue and continue to the next one
4418+
continue;
44144419
}
44154420
if (typeof inst[req[arg]] === "function") {
44164421
inst[req[arg]].apply(inst, req.slice(arg + 1));

0 commit comments

Comments
 (0)