Skip to content

Commit 379f079

Browse files
Code review
1 parent 39048be commit 379f079

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

src/autocapture/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ Autocapture.prototype.trackDomEvent = function(ev, mpEventName) {
160160
blockElementCallback: this.getConfig(CONFIG_BLOCK_ELEMENT_CALLBACK),
161161
blockSelectors: this.getConfig(CONFIG_BLOCK_SELECTORS),
162162
captureExtraAttrs: this.getConfig(CONFIG_CAPTURE_EXTRA_ATTRS),
163-
captureTextContent: this.getConfig(CONFIG_CAPTURE_TEXT_CONTENT)
163+
captureTextContent: this.getConfig(CONFIG_CAPTURE_TEXT_CONTENT),
164+
capturedForHeatMap: mpEventName === MP_EV_CLICK && !this.getConfig(CONFIG_TRACK_CLICK) && this.mp.get_active_record_heatmap_data(),
164165
});
165166
if (props) {
166167
_.extend(props, DEFAULT_PROPS);
@@ -171,13 +172,13 @@ Autocapture.prototype.trackDomEvent = function(ev, mpEventName) {
171172
Autocapture.prototype.initClickTracking = function() {
172173
window.removeEventListener(EV_CLICK, this.listenerClick);
173174

174-
if (!this.getConfig(CONFIG_TRACK_CLICK) && !this.mp.get_active_record_capture_interactions()) {
175+
if (!this.getConfig(CONFIG_TRACK_CLICK) && !this.mp.get_config('record_heatmap_data')) {
175176
return;
176177
}
177178
logger.log('Initializing click tracking');
178179

179180
this.listenerClick = window.addEventListener(EV_CLICK, function(ev) {
180-
if (!this.getConfig(CONFIG_TRACK_CLICK) && !this.mp.get_active_record_capture_interactions()) {
181+
if (!this.getConfig(CONFIG_TRACK_CLICK) && !this.mp.get_active_record_heatmap_data()) {
181182
return;
182183
}
183184
this.trackDomEvent(ev, MP_EV_CLICK);

src/autocapture/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ function getPropsForDOMEvent(ev, config) {
114114
var blockSelectors = config.blockSelectors || [];
115115
var captureTextContent = config.captureTextContent || false;
116116
var captureExtraAttrs = config.captureExtraAttrs || [];
117+
var capturedForHeatMap = config.capturedForHeatMap || false;
117118

118119
// convert array to set every time, as the config may have changed
119120
var blockAttrsSet = {};
@@ -192,6 +193,7 @@ function getPropsForDOMEvent(ev, config) {
192193
props['$' + prop] = ev[prop];
193194
}
194195
});
196+
props['$captured_for_heatmap'] = capturedForHeatMap;
195197
target = guessRealClickTarget(ev);
196198
}
197199
// prioritize text content from "real" click target if different from original target

src/mixpanel-core.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ var DEFAULT_CONFIG = {
152152
'record_mask_text_selector': '*',
153153
'record_max_ms': MAX_RECORDING_MS,
154154
'record_min_ms': 0,
155-
'record_capture_interactions': false,
155+
'record_heatmap_data': false,
156156
'record_sessions_percent': 0,
157157
'recorder_src': 'https://cdn.mxpnl.com/libs/mixpanel-recorder.min.js'
158158
};
@@ -488,11 +488,8 @@ MixpanelLib.prototype.resume_session_recording = function () {
488488
}
489489
};
490490

491-
MixpanelLib.prototype.get_active_record_capture_interactions = function () {
492-
if (this._recorder) {
493-
return this._recorder['getActiveReplayId']() && this.get_config('record_capture_interactions');
494-
}
495-
return false;
491+
MixpanelLib.prototype.get_active_record_heatmap_data = function () {
492+
return this._get_session_replay_id() && this.get_config('record_heatmap_data');
496493
};
497494

498495
MixpanelLib.prototype.get_session_recording_properties = function () {
@@ -1850,7 +1847,7 @@ MixpanelLib.prototype.set_config = function(config) {
18501847
}
18511848
Config.DEBUG = Config.DEBUG || this.get_config('debug');
18521849

1853-
if (('autocapture' in config || this.get_config('record_capture_interactions')) && this.autocapture) {
1850+
if (('autocapture' in config || 'record_heatmap_data' in config) && this.autocapture) {
18541851
this.autocapture.init();
18551852
}
18561853
}

tests/test.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3891,7 +3891,7 @@
38913891
same(this.requests.length, 0, "autocapture init with pageview=false should not fire request on load");
38923892
});
38933893

3894-
test("autocapture tracks click events", 4, function() {
3894+
test("autocapture tracks click events", 5, function() {
38953895
mixpanel.init("autocapture_test_token", {
38963896
autocapture: {
38973897
pageview: false,
@@ -3912,6 +3912,7 @@
39123912
same(this.requests.length, 1, "click event should fire request");
39133913
var last_event = getRequestData(this.requests[0]);
39143914
same(last_event.event, "$mp_click", "last request should be $mp_click event");
3915+
same(last_event.properties.$captured_for_heatmap, false, "click event should not be tagged as captured for heatmap");
39153916
ok(originalHandlerCalled, "original click handler should also have been called");
39163917
});
39173918

@@ -6184,6 +6185,14 @@
61846185
this.randomStub = sinon.stub(Math, 'random');
61856186
this.fetchStub = sinon.stub(window, 'fetch');
61866187

6188+
if (USE_XHR) {
6189+
this.xhr = sinon.useFakeXMLHttpRequest();
6190+
this.requests = [];
6191+
this.xhr.onCreate = _.bind(function(req) {
6192+
this.requests.push(req);
6193+
}, this);
6194+
}
6195+
61876196
// realistic successful response
61886197
this.fetchStub.returns(Promise.resolve(new Response(JSON.stringify({code: 200, status: 'OK'}), {
61896198
status: 200,
@@ -6324,6 +6333,10 @@
63246333
this.responseBlobStub.restore();
63256334
}
63266335

6336+
if (USE_XHR) {
6337+
this.xhr.restore();
6338+
}
6339+
63276340
var scriptEl = this.getRecorderScript();
63286341
if (scriptEl) {
63296342
scriptEl.parentNode.removeChild(scriptEl);
@@ -6547,6 +6560,51 @@
65476560
}, this))
65486561
});
65496562

6563+
if (USE_XHR) {
6564+
asyncTest('does not track clicks when using recording and capturing heat map data is off', function () {
6565+
this.randomStub.returns(0.02);
6566+
this.initMixpanelRecorder({record_sessions_percent: 100, record_heatmap_data: false, batch_requests: false});
6567+
this.waitForRecorderLoad()
6568+
.then(_.bind(function () {
6569+
var e1 = ele_with_class();
6570+
var originalHandlerCalled = false;
6571+
e1.e.onclick = function() {
6572+
originalHandlerCalled = true;
6573+
return false;
6574+
}
6575+
6576+
simulateMouseClick(e1.e);
6577+
6578+
same(this.requests.length, 0, "click event should not fire request");
6579+
ok(originalHandlerCalled, "original click handler should also have been called");
6580+
}, this))
6581+
.then(start)
6582+
});
6583+
6584+
asyncTest('tracks clicks when using recording and capturing capturing heat map data is on', function () {
6585+
this.randomStub.returns(0.02);
6586+
this.initMixpanelRecorder({record_sessions_percent: 100, record_heatmap_data: true, batch_requests: false});
6587+
this.waitForRecorderLoad()
6588+
.then(_.bind(function () {
6589+
var e1 = ele_with_class();
6590+
var originalHandlerCalled = false;
6591+
e1.e.onclick = function() {
6592+
originalHandlerCalled = true;
6593+
return false;
6594+
}
6595+
6596+
simulateMouseClick(e1.e);
6597+
6598+
same(this.requests.length, 1, "click event should fire request");
6599+
var last_event = getRequestData(this.requests[0]);
6600+
same(last_event.event, "$mp_click", "last request should be $mp_click event");
6601+
same(last_event.properties.$captured_for_heatmap, true, "last request should be tagged as captured for heat map");
6602+
ok(originalHandlerCalled, "original click handler should also have been called");
6603+
}, this))
6604+
.then(start)
6605+
});
6606+
}
6607+
65506608
test('respects tracking opt-out when sampled', 3, function () {
65516609
this.randomStub.returns(0.02);
65526610
this.initMixpanelRecorder({record_sessions_percent: 10, window: {navigator: {doNotTrack: '1'}}});

0 commit comments

Comments
 (0)