Skip to content

Commit f9cfe57

Browse files
committed
support non-b64 payloads and default to it for the normal api hosts
1 parent 674182f commit f9cfe57

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/mixpanel-core.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ var IDENTITY_FUNC = function(x) {return x;};
5656
var NOOP_FUNC = function() {};
5757

5858
/** @const */ var PRIMARY_INSTANCE_NAME = 'mixpanel';
59+
/** @const */ var PAYLOAD_TYPE_BASE64 = 'base64';
60+
/** @const */ var PAYLOAD_TYPE_JSON = 'json';
5961

6062

6163
/*
@@ -86,6 +88,7 @@ var DEFAULT_CONFIG = {
8688
'api_host': 'https://api-js.mixpanel.com',
8789
'api_method': 'POST',
8890
'api_transport': 'XHR',
91+
'api_payload_format': PAYLOAD_TYPE_BASE64,
8992
'app_host': 'https://mixpanel.com',
9093
'cdn': 'https://cdn.mxpnl.com',
9194
'cross_site_cookie': false,
@@ -181,12 +184,6 @@ var create_mplib = function(token, config, name) {
181184
return instance;
182185
};
183186

184-
var encode_data_for_request = function(data) {
185-
var json_data = _.JSONEncode(data);
186-
var encoded_data = _.base64Encode(json_data);
187-
return {'data': encoded_data};
188-
};
189-
190187
// Initialization methods
191188

192189
/**
@@ -236,7 +233,17 @@ MixpanelLib.prototype._init = function(token, config, name) {
236233
this['config'] = {};
237234
this['_triggered_notifs'] = [];
238235

239-
this.set_config(_.extend({}, DEFAULT_CONFIG, config, {
236+
var variable_features = {};
237+
238+
// default to JSON payload for standard mixpanel.com API hosts
239+
if (!('api_payload_format' in config)) {
240+
var api_host = config['api_host'] || DEFAULT_CONFIG['api_host'];
241+
if (api_host.match(/\.mixpanel\.com$/)) {
242+
variable_features['api_payload_format'] = PAYLOAD_TYPE_JSON;
243+
}
244+
}
245+
246+
this.set_config(_.extend({}, DEFAULT_CONFIG, variable_features, config, {
240247
'name': name,
241248
'token': token,
242249
'callback_fn': ((name === PRIMARY_INSTANCE_NAME) ? name : PRIMARY_INSTANCE_NAME + '.' + name) + '._jsc'
@@ -612,7 +619,7 @@ MixpanelLib.prototype.init_batchers = function() {
612619
sendRequestFunc: _.bind(function(data, options, cb) {
613620
this._send_request(
614621
this.get_config('api_host') + attrs.endpoint,
615-
encode_data_for_request(data),
622+
this._encode_data_for_request(data),
616623
options,
617624
this._prepare_callback(cb, data)
618625
);
@@ -686,6 +693,14 @@ MixpanelLib.prototype.disable = function(events) {
686693
}
687694
};
688695

696+
MixpanelLib.prototype._encode_data_for_request = function(data) {
697+
var encoded_data = _.JSONEncode(data);
698+
if (this.get_config('api_payload_format') === PAYLOAD_TYPE_BASE64) {
699+
encoded_data = _.base64Encode(encoded_data);
700+
}
701+
return {'data': encoded_data};
702+
};
703+
689704
// internal method for handling track vs batch-enqueue logic
690705
MixpanelLib.prototype._track_or_batch = function(options, callback) {
691706
var truncated_data = _.truncate(options.data, 255);
@@ -705,7 +720,7 @@ MixpanelLib.prototype._track_or_batch = function(options, callback) {
705720
console.log(truncated_data);
706721
return this._send_request(
707722
endpoint,
708-
encode_data_for_request(truncated_data),
723+
this._encode_data_for_request(truncated_data),
709724
send_request_options,
710725
this._prepare_callback(callback, truncated_data)
711726
);

0 commit comments

Comments
 (0)