Skip to content

Commit

Permalink
[Fix] tracking errors async (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
turtledreams committed Mar 26, 2024
1 parent 3b06efa commit 53998e5
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 99 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 23.12.6
- Mitigated an issue where error tracking could prevent SDK initialization in async mode

## 23.12.5
- Mitigated an issue where the SDK was not emptying the async queue explicity when closing a browser

Expand Down
45 changes: 3 additions & 42 deletions cypress/integration/async_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ describe("Test Countly.q related methods and processes", () => {
expect(Countly.q.length).to.equal(0);

// Add 4 events to the .q
Countly.q.push(["track_errors"]); // adding this as calling it during init used to cause an error (at v23.12.5)
Countly.q.push(["add_event", event(1)]);
Countly.q.push(["add_event", event(2)]);
Countly.q.push(["add_event", event(3)]);
Countly.q.push(["add_event", event(4)]);
// Check that the .q has 4 events
expect(Countly.q.length).to.equal(4);
expect(Countly.q.length).to.equal(5);

cy.fetch_local_event_queue().then((rq) => {
// Check that events are still in .q
expect(Countly.q.length).to.equal(4);
expect(Countly.q.length).to.equal(5);

// Check that the event queue is empty
expect(rq.length).to.equal(0);
Expand Down Expand Up @@ -139,46 +140,6 @@ describe("Test Countly.q related methods and processes", () => {
});
});
});
// 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)
it("Check clear_stored_id set to true empties the .q", () => {
hp.haltAndClearStorage(() => {
// Disable heartbeat
Countly.noHeartBeat = true;
Countly.q = [];
localStorage.setItem("YOUR_APP_KEY/cly_id", "old_user_id"); // Set old device ID for clear_stored_id to work

// Add 4 events to the .q
Countly.q.push(["add_event", event(1)]);
Countly.q.push(["add_event", event(2)]);
Countly.q.push(["add_event", event(3)]);
Countly.q.push(["add_event", event(4)]);

// Check that the .q has 4 events
expect(Countly.q.length).to.equal(4);

// Init the SDK with clear_stored_id set to true
initMain(true);
cy.wait(1000);

// Check that the .q is empty
expect(Countly.q.length).to.equal(0);

cy.fetch_local_event_queue().then((rq) => {
// Check that the event queue is empty because processAsyncQueue sends events from .q to event queue and then to request queue
expect(rq.length).to.equal(0);

cy.fetch_local_request_queue().then((rq_2) => {
// Check that events are now in request queue
expect(rq_2.length).to.equal(1);
const eventsArray = JSON.parse(rq_2[0].events);
expect(eventsArray[0].key).to.equal("event_1");
expect(eventsArray[1].key).to.equal("event_2");
expect(eventsArray[2].key).to.equal("event_3");
expect(eventsArray[3].key).to.equal("event_4");
});
});
});
});
// This test checks if calling user_details triggers processAsyncQueue (it sends events from .q to event queue and then to request queue)
it("Check sending user details empties .q", () => {
hp.haltAndClearStorage(() => {
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/bridge_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function initMain(name, version) {
}

const SDK_NAME = "javascript_native_web";
const SDK_VERSION = "23.12.5";
const SDK_VERSION = "23.12.6";

// tests
describe("Bridged SDK Utilities Tests", () => {
Expand Down
1 change: 1 addition & 0 deletions cypress/integration/utm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("UTM tests ", () => {
it("Checks if a single default utm tag works", () => {
hp.haltAndClearStorage(() => {
initMulti("YOUR_APP_KEY", "?utm_source=hehe", undefined);
Countly.q.push(["track_errors"]); // adding this as calling it during init used to cause an error (at v23.12.5)
cy.fetch_local_request_queue().then((rq) => {
cy.log(rq);
const custom = JSON.parse(rq[0].user_details).custom;
Expand Down
13 changes: 9 additions & 4 deletions lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
statusCode: "cly_hc_status_code",
errorMessage: "cly_hc_error_message"
});
var SDK_VERSION = "23.12.5";
var SDK_VERSION = "23.12.6";
var SDK_NAME = "javascript_native_web";

// 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)
Expand Down Expand Up @@ -956,8 +956,7 @@
log(logLevelEnums.DEBUG, "initialize, No device ID type info from the previous session, falling back to DEVELOPER_SUPPLIED, for event flushing");
deviceIdType = DeviceIdTypeInternalEnums.DEVELOPER_SUPPLIED;
}
// process async queue before sending events
processAsyncQueue();
// don't process async queue here, just send the events (most likely async data is for the new user)
sendEventsForced();
// set them back to their initial values
this.device_id = undefined;
Expand Down Expand Up @@ -1148,7 +1147,6 @@
if (sessionCookieTimeout !== configurationDefaultValues.SESSION_COOKIE_TIMEOUT) {
log(logLevelEnums.DEBUG, "initialize, session_cookie_timeout set to:[" + sessionCookieTimeout + "] minutes to expire a cookies session");
}
log(logLevelEnums.INFO, "initialize, Countly initialized");
var deviceIdParamValue = null;
var searchQuery = self.getSearchQuery();
var hasUTM = false;
Expand Down Expand Up @@ -1274,6 +1272,7 @@
}
// send instant health check request
HealthCheck.sendInstantHCRequest();
log(logLevelEnums.INFO, "initialize, Countly initialized");
};

/**
Expand Down Expand Up @@ -4392,6 +4391,10 @@
* @memberof Countly._internals
*/
function processAsyncQueue() {
if (typeof Countly === "undefined" || typeof Countly.i === "undefined") {
log(logLevelEnums.DEBUG, "Countly is not finished initialization yet, will process the queue after initialization is done");
return;
}
var q = Countly.q;
Countly.q = [];
for (var i = 0; i < q.length; i++) {
Expand All @@ -4411,6 +4414,8 @@
} catch (error) {
// possibly first init and no other instance
log(logLevelEnums.DEBUG, "No instance found for the provided key while processing async queue");
Countly.q.push(req); // return it back to queue and continue to the next one
continue;
}
if (typeof inst[req[arg]] === "function") {
inst[req[arg]].apply(inst, req.slice(arg + 1));
Expand Down
Loading

0 comments on commit 53998e5

Please sign in to comment.