Skip to content

Commit

Permalink
Fix page view firing logic for path changes only
Browse files Browse the repository at this point in the history
  • Loading branch information
chiyc committed Feb 1, 2024
1 parent 50d138c commit 038d2fc
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dist/mixpanel.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4511,7 +4511,7 @@ define(function () { 'use strict';
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
2 changes: 1 addition & 1 deletion dist/mixpanel.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4511,7 +4511,7 @@ MixpanelLib.prototype._init_url_change_tracking = function(track_pageview_option
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
2 changes: 1 addition & 1 deletion dist/mixpanel.globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4512,7 +4512,7 @@
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
48 changes: 24 additions & 24 deletions dist/mixpanel.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mixpanel.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4515,7 +4515,7 @@
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
2 changes: 1 addition & 1 deletion examples/commonjs-browserify/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4525,7 +4525,7 @@ MixpanelLib.prototype._init_url_change_tracking = function(track_pageview_option
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
2 changes: 1 addition & 1 deletion examples/es2015-babelify/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ MixpanelLib.prototype._init_url_change_tracking = function (track_pageview_optio
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
2 changes: 1 addition & 1 deletion examples/umd-webpack/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4578,7 +4578,7 @@
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
2 changes: 1 addition & 1 deletion src/mixpanel-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ MixpanelLib.prototype._init_url_change_tracking = function(track_pageview_option
} else if (track_pageview_option === 'url-with-path-and-query-string') {
should_track = current_url.split('#')[0] !== previous_tracked_url.split('#')[0];
} else if (track_pageview_option === 'url-with-path') {
should_track = current_url.split('?')[0] !== previous_tracked_url.split('?')[0];
should_track = current_url.split('#')[0].split('?')[0] !== previous_tracked_url.split('#')[0].split('?')[0];
}

if (should_track) {
Expand Down
33 changes: 31 additions & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3811,6 +3811,9 @@
});

test("init with track_pageview=true fires page view event", 2, function() {
var next_url = window.location.protocol + "//" + window.location.host + window.location.pathname
window.history.pushState({ path: next_url }, '', next_url);

mixpanel.init("track_pageview_test_token", {
track_pageview: true,
batch_requests: false,
Expand All @@ -3820,7 +3823,33 @@
same(last_event.event, "$mp_web_page_view", "last request should be $mp_web_page_view event");
});

test("init with track_pageview='url-with-path' tracks page views correctly", 5, function() {
var next_url = window.location.protocol + "//" + window.location.host + window.location.pathname
window.history.pushState({ path: next_url }, '', next_url);

mixpanel.init("track_pageview_test_token", {
track_pageview: "url-with-path",
batch_requests: false,
}, 'pageviews_pathonly');
same(this.requests.length, 1, "init with track_pageview='url-with-path' should fire single request on load");
var last_event = getRequestData(this.requests[0]);
same(last_event.event, "$mp_web_page_view", "last request should be $mp_web_page_view event");

window.dispatchEvent(new Event("mp_locationchange"));
same(this.requests.length, 1, "mp_locationchange event should not fire page view event on no URL change");

window.location.href = window.location.href.split('#') + '#anchor'
same(this.requests.length, 1, "init with track_pageview='url-with-path' should not fire additional request on hash change");

var next_url = window.location.protocol + "//" + window.location.host + window.location.pathname + '?hello=world';
window.history.pushState({ path: next_url }, '', next_url);
same(this.requests.length, 1, "init with track_pageview='url-with-path' should not fire request on query string change");
});

test("init with track_pageview='url-with-path-and-query-string' tracks page views correctly", 6, function() {
var next_url = window.location.protocol + "//" + window.location.host + window.location.pathname
window.history.pushState({ path: next_url }, '', next_url);

mixpanel.init("track_pageview_test_token", {
track_pageview: "url-with-path-and-query-string",
batch_requests: false,
Expand All @@ -3832,13 +3861,13 @@
window.dispatchEvent(new Event("mp_locationchange"));
same(this.requests.length, 1, "mp_locationchange event should not fire page view event on no URL change");

var next_url = window.location.protocol + "//" + window.location.host + window.location.pathname + '?hello=world';
var next_url = window.location.protocol + "//" + window.location.host + window.location.pathname + '?next=query';
window.history.pushState({ path: next_url }, '', next_url);
same(this.requests.length, 2, "init with track_pageview='url-with-path-and-query-string' should fire request on query string change");
var last_event = getRequestData(this.requests[1]);
same(last_event.event, "$mp_web_page_view", "last request should be $mp_web_page_view event");

window.location.href = window.location.href.split('#')[0] + '#anchor'
window.location.href = window.location.href.split('#')[0] + '#anotheranchor'
same(this.requests.length, 2, "init with track_pageview='url-with-path-and-query-string' should not fire additional request on hash change");
});
}
Expand Down

0 comments on commit 038d2fc

Please sign in to comment.