From 9ce64ac43adf7c425467b4561353ad4702d4a589 Mon Sep 17 00:00:00 2001 From: turtledreams Date: Fri, 15 Dec 2023 17:42:11 +0900 Subject: [PATCH 1/3] Override SDK version and name --- CHANGELOG.md | 4 + cypress/e2e/bridged_utils.cy.js | 44 +++++ cypress/e2e/views.cy copy.js | 305 -------------------------------- modules/Constants.js | 2 +- modules/CountlyClass.js | 16 +- package.json | 2 +- 6 files changed, 59 insertions(+), 314 deletions(-) create mode 100644 cypress/e2e/bridged_utils.cy.js delete mode 100644 cypress/e2e/views.cy copy.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e4b79e..13308b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 23.12.1 + +* Added methods for bridged SDK usage + ## 23.12.0 * Modularized the Web SDK diff --git a/cypress/e2e/bridged_utils.cy.js b/cypress/e2e/bridged_utils.cy.js new file mode 100644 index 0000000..33debfc --- /dev/null +++ b/cypress/e2e/bridged_utils.cy.js @@ -0,0 +1,44 @@ +/* eslint-disable cypress/no-unnecessary-waiting */ +/* eslint-disable require-jsdoc */ +var Countly = require("../../Countly.js"); +import { SDK_NAME, SDK_VERSION } from "../../modules/Constants.js"; +// import * as Countly from "../../dist/countly_umd.js"; +var hp = require("../support/helper.js"); + +function initMain(name, version) { + Countly.init({ + app_key: "YOUR_APP_KEY", + url: "https://your.domain.count.ly", + test_mode: true, + debug: true, + sdk_name: name, + sdk_version: version + }); +} + + +// tests +describe("Bridged SDK Utilities Tests", () => { + it("Check if we can override sdk name and version successful", () => { + hp.haltAndClearStorage(() => { + initMain('javascript_gtm_web', '24.0.0'); + hp.events(); + cy.fetch_local_request_queue().then((eq) => { + expect(eq).to.have.length(1); + expect(eq[0].sdk_name).to.equal("javascript_gtm_web"); + expect(eq[0].sdk_version).to.equal("24.0.0"); + }); + }); + }); + it("Check if SDK uses default values if SDK name and version was not overriden", () => { + hp.haltAndClearStorage(() => { + initMain(undefined, undefined); + hp.events(); + cy.fetch_local_request_queue().then((eq) => { + expect(eq).to.have.length(1); + expect(eq[0].sdk_name).to.equal(SDK_NAME); + expect(eq[0].sdk_version).to.equal(SDK_VERSION); + }); + }); + }); +}); \ No newline at end of file diff --git a/cypress/e2e/views.cy copy.js b/cypress/e2e/views.cy copy.js deleted file mode 100644 index b5d8eba..0000000 --- a/cypress/e2e/views.cy copy.js +++ /dev/null @@ -1,305 +0,0 @@ -/* eslint-disable cypress/no-unnecessary-waiting */ -/* eslint-disable require-jsdoc */ -var Countly = require("../../Countly.js"); -var hp = require("../support/helper"); - -function initMain() { - Countly.init({ - app_key: "YOUR_APP_KEY", - url: "https://your.domain.count.ly", - test_mode_eq: true, - test_mode: true - }); -} - -/** - * Checks if the cvid is the same for all events in the queue but ids are different and pvid is undefined - * @param {string} expectedCvid - expected view id - * @param {Array} eventQ - events queue - * @param {number} startIndex - start index of the queue - * @param {number} endIndex - end index of the queue -*/ -function listIdChecker(expectedCvid, eventQ, startIndex, endIndex) { - if (!endIndex || !startIndex || endIndex < startIndex) { // prevent infinite loop - cy.log("Wrong index information"); - return; - } - var i = startIndex; - var lastIdList = []; // pool of ids - while (i < endIndex) { - expect(eventQ[i].cvid).to.equal(expectedCvid); - expect(eventQ[i].pvid).to.be.undefined; // there should not be pvid - if (lastIdList.length > 0) { - expect(lastIdList.indexOf(eventQ[i].id)).to.equal(-1); // we check this id against all ids in the list - } - lastIdList.push(eventQ[i].id); // we add this id to the list of ids - i++; - } -} - -var pageNameOne = "test view page name1"; -var pageNameTwo = "test view page name2"; - -describe("View ID tests ", () => { - it("Checks if UUID and secureRandom works as intended", () => { - hp.haltAndClearStorage(() => { - initMain(); - const uuid = Countly._internals.generateUUID(); - const id = Countly._internals.secureRandom(); - assert.equal(uuid.length, 36); - assert.equal(id.length, 21); - const uuid2 = Countly._internals.generateUUID(); - const id2 = Countly._internals.secureRandom(); - assert.equal(uuid2.length, 36); - assert.equal(id2.length, 21); - assert.notEqual(uuid, uuid2); - assert.notEqual(id, id2); - }); - }); - it("Checks if recording page view works", () => { - hp.haltAndClearStorage(() => { - initMain(); - Countly.track_view(pageNameOne); - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(1); - cy.check_view_event(eq[0], pageNameOne, undefined, false); - }); - }); - }); - it("Checks if recording timed page views with same name works", () => { - hp.haltAndClearStorage(() => { - initMain(); - Countly.track_view(pageNameOne); - cy.wait(3000).then(() => { - Countly.track_view(pageNameOne); - cy.fetch_local_event_queue().then((eq) => { - cy.log(eq); - expect(eq.length).to.equal(3); - cy.check_view_event(eq[0], pageNameOne, undefined, false); - const id1 = eq[0].id; - - cy.check_view_event(eq[1], pageNameOne, 3, false); - const id2 = eq[1].id; - assert.equal(id1, id2); - - cy.check_view_event(eq[2], pageNameOne, undefined, true); - const id3 = eq[2].id; - const pvid = eq[2].pvid; - assert.equal(id1, pvid); - assert.notEqual(id3, pvid); - }); - }); - }); - }); - it("Checks if recording timed page views with different name works", () => { - hp.haltAndClearStorage(() => { - initMain(); - Countly.track_view(pageNameOne); - hp.waitFunction(hp.getTimestampMs(), 4000, 500, ()=>{ - Countly.track_view(pageNameTwo); - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(3); - cy.check_view_event(eq[0], pageNameOne, undefined, false); - const id1 = eq[0].id; - - // this test is flaky we are expecting 3 and +1 (4) to make test more reliable - cy.check_view_event(eq[1], pageNameOne, 4, false); - const id2 = eq[1].id; - assert.equal(id1, id2); - - cy.check_view_event(eq[2], pageNameTwo, undefined, true); - const id3 = eq[2].id; - const pvid = eq[2].pvid; - assert.equal(id1, pvid); - assert.notEqual(id3, pvid); - }); - }); - }); - }); - - // =========================== - // Confirms: - // view A's id and event A's cvid are same - // view B's id and event B's cvid are same. Also view B's pvid and view A's id are same - // view C's id and event C's cvid are same. Also view C's pvid and view B's id are same - // - // request order: view A start -> internal can custom events -> event A -> view A end -> view B start -> internal can custom events -> event B -> view B end -> view C start -> internal can custom events -> event C - // =========================== - it("Checks a sequence of events and page views", () => { - hp.haltAndClearStorage(() => { - initMain(); - Countly.track_view("A"); - hp.events(["[CLY]_view"]); - Countly.add_event({ key: "A" }); - Countly.track_view("B"); - hp.events(["[CLY]_view"]); - - Countly.add_event({ key: "B" }); - Countly.track_view("C"); - hp.events(["[CLY]_view"]); - Countly.add_event({ key: "C" }); - - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(26); - cy.log(eq); - - // event A and view A - cy.check_view_event(eq[0], "A", undefined, false); // no pvid - const idA = eq[0].id; // idA - listIdChecker(idA, eq, 1, 7); // check all internal events in view A - cy.check_event(eq[7], { key: "A" }, undefined, idA); // cvid should be idA - cy.check_view_event(eq[8], "A", 0, false); // no pvid - - // event B and view B - cy.check_view_event(eq[9], "B", undefined, idA); // pvid is idA - const idB = eq[9].id; // idB - listIdChecker(idB, eq, 10, 16); // check all internal events in view B - cy.check_event(eq[16], { key: "B" }, undefined, idB); // cvid should be idB - cy.check_view_event(eq[17], "B", 0, idA); // pvid is idA - - // event C and view C - cy.check_view_event(eq[18], "C", undefined, idB); // pvid is idB - const idC = eq[18].id; // idC - listIdChecker(idC, eq, 19, 25); // check all internal events in view C - cy.check_event(eq[25], { key: "C" }, undefined, idC); // cvid should be idC - }); - }); - }); - - // =========================== - // Confirms: CVID | PVID | ID - // ++--------+-----------+-------++ - // record events before first view => "" undefined rnd - // record A view => undefined "" idA - // record events under view A => idA undefined rnd - // record A view (close) => undefined "" idA - // record B view => undefined idA idB - // record events under view B => idB undefined rnd - // record B view (close) => undefined idA idB - // record C view => undefined idB idC - // record events under view C => idC undefined rnd - // ++--------+-----------+-------++ - // request order: internal can custom events -> view A start -> event A -> view A end -> view B start -> internal can custom events -> event B -> view B end -> view C start -> internal can custom events -> event C - // =========================== - it("Checks a sequence of events and page views, with events before first view", () => { - hp.haltAndClearStorage(() => { - initMain(); - hp.events(["[CLY]_view"]); // first events - - Countly.track_view("A"); - Countly.add_event({ key: "A" }); - Countly.track_view("B"); - hp.events(["[CLY]_view"]); - - Countly.add_event({ key: "B" }); - Countly.track_view("C"); - hp.events(["[CLY]_view"]); - Countly.add_event({ key: "C" }); - - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(26); - cy.log(eq); - - listIdChecker("", eq, 0, 6); // check all internal events before view A - - // event A and view A - cy.check_view_event(eq[6], "A", undefined, false); // no pvid - const idA = eq[6].id; // idA - cy.check_event(eq[7], { key: "A" }, undefined, idA); // cvid should be idA - cy.check_view_event(eq[8], "A", 0, false); // no pvid - - // event B and view B - cy.check_view_event(eq[9], "B", undefined, idA); // pvid is idA - const idB = eq[9].id; // idB - listIdChecker(idB, eq, 10, 16); // check all internal events in view B - cy.check_event(eq[16], { key: "B" }, undefined, idB); // cvid should be idB - cy.check_view_event(eq[17], "B", 0, idA); // pvid is idA - - // event C and view C - cy.check_view_event(eq[18], "C", undefined, idB); // pvid is idB - const idC = eq[18].id; // idC - listIdChecker(idC, eq, 19, 25); // check all internal events in view C - cy.check_event(eq[25], { key: "C" }, undefined, idC); // cvid should be idC - }); - }); - }); - - // check end_session usage - it("Checks a sequence of events and page views, with end_session, no session started", () => { - hp.haltAndClearStorage(() => { - initMain(); - hp.events(["[CLY]_view"]); // first events - Countly.end_session(); // no session started must be ignored - Countly.track_view("A"); - Countly.add_event({ key: "A" }); - - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(8); - cy.log(eq); - - listIdChecker("", eq, 0, 6); // check all internal events before view A - - // event A and view A - cy.check_view_event(eq[6], "A", undefined, false); // no pvid - const idA = eq[6].id; // idA - cy.check_event(eq[7], { key: "A" }, undefined, idA); // cvid should be idA - }); - }); - }); - it("Checks a sequence of events and page views, with end_session, with session started", () => { - hp.haltAndClearStorage(() => { - initMain(); - Countly.track_sessions(); - hp.events(["[CLY]_view"]); // first events - Countly.end_session(); // no view started so must be ignored - Countly.track_view("A"); - Countly.add_event({ key: "A" }); - - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(9); // orientation added - cy.log(eq); - - cy.check_event(eq[0], { key: "[CLY]_orientation" }, undefined, ""); // internal event - - listIdChecker("", eq, 1, 7); // check all internal events before view A - - // event A and view A - cy.check_view_event(eq[7], "A", undefined, false); // no pvid - const idA = eq[7].id; // idA - cy.check_event(eq[8], { key: "A" }, undefined, idA); // cvid should be idA - }); - }); - }); - it("Checks a sequence of events and page views, with end_session, with session started and called after view", () => { - hp.haltAndClearStorage(() => { - initMain(); - Countly.track_sessions(); - hp.events(["[CLY]_view"]); // first events - Countly.track_view("A"); - Countly.end_session(); // no view started so must be ignored - Countly.add_event({ key: "A" }); - - Countly.track_view("B"); - hp.events(["[CLY]_view"]); - - cy.fetch_local_event_queue().then((eq) => { - expect(eq.length).to.equal(17); // orientation added - cy.log(eq); - - cy.check_event(eq[0], { key: "[CLY]_orientation" }, undefined, ""); // internal event - - listIdChecker("", eq, 1, 7); // check all internal events before view A - - // event A and view A - cy.check_view_event(eq[7], "A", undefined, false); // no pvid - const idA = eq[7].id; // idA - cy.check_view_event(eq[8], "A", 0, false); // no pvid - cy.check_event(eq[9], { key: "A" }, undefined, idA); // cvid should be idA - - cy.check_view_event(eq[10], "B", undefined, idA); // pvid is idA - const idB = eq[10].id; // idB - listIdChecker(idB, eq, 11, 17); // check all internal events in view B - }); - }); - }); -}); \ No newline at end of file diff --git a/modules/Constants.js b/modules/Constants.js index bc17a16..43c4fcf 100644 --- a/modules/Constants.js +++ b/modules/Constants.js @@ -104,7 +104,7 @@ var healthCheckCounterEnum = Object.freeze({ errorMessage: "cly_hc_error_message", }); -var SDK_VERSION = "23.6.3"; +var SDK_VERSION = "23.12.1"; var SDK_NAME = "javascript_native_web"; // Using this on document.referrer would return an array with 15 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) diff --git a/modules/CountlyClass.js b/modules/CountlyClass.js index 3dea2cf..2509cbf 100644 --- a/modules/CountlyClass.js +++ b/modules/CountlyClass.js @@ -80,6 +80,8 @@ class CountlyClass { var currentViewId = null; // this is the global variable for tracking the current view's ID. Used in view tracking. Becomes previous view ID at the end. var previousViewId = null; // this is the global variable for tracking the previous view's ID. Used in view tracking. First view has no previous view ID. var freshUTMTags = null; + var sdkName = getConfig("sdk_name", ob, SDK_NAME); + var sdkVersion = getConfig("sdk_version", ob, SDK_VERSION); try { localStorage.setItem("cly_testLocal", true); @@ -2902,8 +2904,8 @@ class CountlyClass { var data = { widget_id: CountlyFeedbackWidget._id, shown: 1, - sdk_version: SDK_VERSION, - sdk_name: SDK_NAME, + sdk_version: sdkVersion, + sdk_name: sdkName, platform: this.platform, app_version: this.app_version }; @@ -3028,10 +3030,10 @@ class CountlyClass { url += "?widget_id=" + presentableFeedback._id; url += "&app_key=" + this.app_key; url += "&device_id=" + this.device_id; - url += "&sdk_name=" + SDK_NAME; + url += "&sdk_name=" + sdkName; url += "&platform=" + this.platform; url += "&app_version=" + this.app_version; - url += "&sdk_version=" + SDK_VERSION; + url += "&sdk_version=" + sdkVersion; if (feedbackWidgetSegmentation) { var customObjectToSendWithTheWidget = {}; customObjectToSendWithTheWidget.sg = feedbackWidgetSegmentation; @@ -3469,7 +3471,7 @@ class CountlyClass { var iframe = document.createElement("iframe"); iframe.name = "countly-feedback-iframe"; iframe.id = "countly-feedback-iframe"; - iframe.src = self.url + "/feedback?widget_id=" + currentWidget._id + "&app_key=" + self.app_key + "&device_id=" + self.device_id + "&sdk_version=" + SDK_VERSION; + iframe.src = self.url + "/feedback?widget_id=" + currentWidget._id + "&app_key=" + self.app_key + "&device_id=" + self.device_id + "&sdk_version=" + sdkVersion; // inject them to dom document.body.appendChild(wrapper); wrapper.appendChild(closeIcon); @@ -3595,8 +3597,8 @@ class CountlyClass { function prepareRequest(request) { request.app_key = self.app_key; request.device_id = self.device_id; - request.sdk_name = SDK_NAME; - request.sdk_version = SDK_VERSION; + request.sdk_name = sdkName; + request.sdk_version = sdkVersion; request.t = deviceIdType; request.av = self.app_version; diff --git a/package.json b/package.json index be20b15..dfaf99d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "countly-sdk-js", - "version": "23.12.0", + "version": "23.12.1", "description": "Countly JavaScript SDK", "type": "module", "main": "Countly.js", From 578e00ca63cc386ceab2aefd278c05a4269c1bac Mon Sep 17 00:00:00 2001 From: turtledreams Date: Fri, 15 Dec 2023 17:55:36 +0900 Subject: [PATCH 2/3] logs --- modules/CountlyClass.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/CountlyClass.js b/modules/CountlyClass.js index 2509cbf..72b28d3 100644 --- a/modules/CountlyClass.js +++ b/modules/CountlyClass.js @@ -259,6 +259,11 @@ class CountlyClass { // init configuration is printed out here: // key values should be printed out as is + if (sdkName === SDK_NAME && sdkVersion === SDK_VERSION) { + log(logLevelEnums.DEBUG, "initialize, SDK name:[" + sdkName + "], version:[" + sdkVersion + "]"); + } else { + log(logLevelEnums.WARNING, "initialize, SDK name:[" + sdkName + "], version:[" + sdkVersion + "], default name:[" + SDK_NAME + "] and default version:[" + SDK_VERSION + "]"); + } log(logLevelEnums.DEBUG, "initialize, app_key:[" + this.app_key + "], url:[" + this.url + "]"); log(logLevelEnums.DEBUG, "initialize, device_id:[" + getConfig("device_id", ob, undefined) + "]"); log(logLevelEnums.DEBUG, "initialize, require_consent is enabled:[" + this.require_consent + "]"); From 46e1aac6ba3a3b15e23ecf49d19464b418eb3b77 Mon Sep 17 00:00:00 2001 From: turtledreams Date: Fri, 15 Dec 2023 20:07:23 +0900 Subject: [PATCH 3/3] warning to debug --- modules/CountlyClass.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/CountlyClass.js b/modules/CountlyClass.js index 72b28d3..decfa0c 100644 --- a/modules/CountlyClass.js +++ b/modules/CountlyClass.js @@ -262,7 +262,7 @@ class CountlyClass { if (sdkName === SDK_NAME && sdkVersion === SDK_VERSION) { log(logLevelEnums.DEBUG, "initialize, SDK name:[" + sdkName + "], version:[" + sdkVersion + "]"); } else { - log(logLevelEnums.WARNING, "initialize, SDK name:[" + sdkName + "], version:[" + sdkVersion + "], default name:[" + SDK_NAME + "] and default version:[" + SDK_VERSION + "]"); + log(logLevelEnums.DEBUG, "initialize, SDK name:[" + sdkName + "], version:[" + sdkVersion + "], default name:[" + SDK_NAME + "] and default version:[" + SDK_VERSION + "]"); } log(logLevelEnums.DEBUG, "initialize, app_key:[" + this.app_key + "], url:[" + this.url + "]"); log(logLevelEnums.DEBUG, "initialize, device_id:[" + getConfig("device_id", ob, undefined) + "]");