Skip to content

Commit 2321ab1

Browse files
authored
feat: make sendTrackingEvents updatable (#405)
* feat: make sendTrackingEvents updatable This may be necessary in case a cookie consent changes and no tracking event should be dispatched anymore. This would allow it to keep the logic therefore in the client and not having to do this check before each call. * test: add tests for setting the sendTrackingEvents property
1 parent 0f32e6c commit 2321ab1

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

spec/src/constructorio.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,34 @@ describe(`ConstructorIO${bundledDescriptionSuffix}`, () => {
279279
expect(instance.options).to.have.property('testCells').to.deep.equal(newTestCells);
280280
});
281281

282+
it('Should update the client options with new sendTrackingEvents value', () => {
283+
const instance = new ConstructorIO({
284+
apiKey: validApiKey,
285+
sendTrackingEvents: true,
286+
});
287+
288+
expect(instance.options).to.have.property('sendTrackingEvents').to.equal(true);
289+
290+
instance.setClientOptions({
291+
sendTrackingEvents: false,
292+
});
293+
294+
expect(instance.options).to.have.property('sendTrackingEvents').to.equal(false);
295+
});
296+
297+
it('Should not update the client options with undefined sendTrackingEvents value', () => {
298+
const instance = new ConstructorIO({
299+
apiKey: validApiKey,
300+
sendTrackingEvents: true,
301+
});
302+
303+
expect(instance.options).to.have.property('sendTrackingEvents').to.equal(true);
304+
305+
instance.setClientOptions({});
306+
307+
expect(instance.options).to.have.property('sendTrackingEvents').to.equal(true);
308+
});
309+
282310
it('Should update the options for modules with new test cells', () => {
283311
const oldTestCells = {
284312
'old-cell-name-1': 'old-cell-value-1',

src/constructorio.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ class ConstructorIO {
158158
* @param {object} [options.testCells] - User test cells
159159
* @param {number} [options.sessionId] - Session ID - Will only be set in DOM-less environments
160160
* @param {string} [options.userId] - User ID
161+
* @param {boolean} [options.sendTrackingEvents] - Indicates if tracking events should be dispatched
161162
*/
162163
setClientOptions(options) {
163164
if (Object.keys(options).length) {
164-
const { apiKey, segments, testCells, sessionId, userId } = options;
165+
const { apiKey, segments, testCells, sessionId, userId, sendTrackingEvents } = options;
165166

166167
if (apiKey) {
167168
this.options.apiKey = apiKey;
@@ -175,6 +176,10 @@ class ConstructorIO {
175176
this.options.testCells = testCells;
176177
}
177178

179+
if (sendTrackingEvents !== undefined) {
180+
this.options.sendTrackingEvents = sendTrackingEvents;
181+
}
182+
178183
// Set Session ID in dom-less environments only
179184
if (sessionId && !helpers.canUseDOM()) {
180185
this.options.sessionId = sessionId;

0 commit comments

Comments
 (0)