Skip to content

Commit

Permalink
Merge pull request #257 from mixpanel/zihe-fix-opt-in-track
Browse files Browse the repository at this point in the history
fix: opt in tracking issue during initialize
  • Loading branch information
zihejia authored Sep 10, 2024
2 parents 6d8da0b + 9688d02 commit a7a1434
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
48 changes: 48 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,54 @@ describe("MixpanelMain", () => {
).toHaveBeenCalledWith(token);
});

it("should not track if initialize with optOutTrackingDefault being true", async () => {
const trackAutomaticEvents = false;
const optOutTrackingDefault = true;
const superProperties = {superProp1: "value1", superProp2: "value2"};
const serverURL = "https://api.mixpanel.com";


await mixpanelMain.initialize(
token,
trackAutomaticEvents,
optOutTrackingDefault,
superProperties,
serverURL
);

const eventName = "Test Event";
const eventProperties = {prop1: "value1", prop2: "value2"};

expect(
mixpanelMain.mixpanelPersistent.updateOptedOut
).toHaveBeenCalledWith(token, true);

mixpanelMain.mixpanelPersistent.getOptedOut.mockReturnValue(true);
await mixpanelMain.track(token, eventName, eventProperties);
expect(mixpanelMain.core.addToMixpanelQueue).not.toHaveBeenCalled();
});

it("should track if initialize with optOutTrackingDefault being false", async () => {
const trackAutomaticEvents = false;
const optOutTrackingDefault = false;
const superProperties = {superProp1: "value1", superProp2: "value2"};
const serverURL = "https://api.mixpanel.com";
console.info("here111");
await mixpanelMain.initialize(
token,
trackAutomaticEvents,
optOutTrackingDefault,
superProperties,
serverURL
);
mixpanelMain.setLoggingEnabled(token, true);
const eventName = "Test Event";
const eventProperties = {prop1: "value1", prop2: "value2"};

await mixpanelMain.track(token, eventName, eventProperties);
expect(mixpanelMain.core.addToMixpanelQueue).toHaveBeenCalled();
});

it("register super properties should update properties", async () => {
mixpanelMain.registerSuperProperties(token, {superProp3: "value3"});
expect(
Expand Down
21 changes: 16 additions & 5 deletions javascript/mixpanel-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class MixpanelMain {
await this.optOutTracking(token);
return;
} else {
await this.optInTracking(token);
await this._setOptedOutTrackingFlag(token, false);
}

this.setServerURL(token, serverURL);
Expand Down Expand Up @@ -71,6 +71,14 @@ export default class MixpanelMain {
}

async track(token, eventName, properties) {
if (this.mixpanelPersistent.getOptedOut(token)) {
MixpanelLogger.log(
token,
`User has opted out of tracking, skipping tracking.`
);
return;
}

MixpanelLogger.log(
token,
`Track '${eventName}' with properties`,
Expand Down Expand Up @@ -133,19 +141,22 @@ export default class MixpanelMain {
}

async optOutTracking(token) {
this.mixpanelPersistent.updateOptedOut(token, true);
await this.mixpanelPersistent.persistOptedOut(token);
await this._setOptedOutTrackingFlag(token, true);
MixpanelLogger.log(token, "User has opted out of tracking");
await this.mixpanelPersistent.reset(token);
}

async optInTracking(token) {
this.mixpanelPersistent.updateOptedOut(token, false);
await this.mixpanelPersistent.persistOptedOut(token);
await this._setOptedOutTrackingFlag(token, false);
MixpanelLogger.log(token, "User has opted in to tracking");
await this.track(token, "$opt_in");
}

async _setOptedOutTrackingFlag(token, optedOut) {
this.mixpanelPersistent.updateOptedOut(token, optedOut);
await this.mixpanelPersistent.persistOptedOut(token);
}

hasOptedOutTracking(token) {
return this.mixpanelPersistent.getOptOut(token);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7a1434

Please sign in to comment.