Skip to content

Commit 4376ba0

Browse files
committed
add tracking options to api_properties
1 parent b17d2b3 commit 4376ba0

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/amplitude-client.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,9 @@ AmplitudeClient.prototype._logEvent = function _logEvent(eventType, eventPropert
896896
_saveCookieData(this);
897897

898898
userProperties = userProperties || {};
899-
apiProperties = merge(apiProperties || {});
899+
var trackingOptions = _generateApiPropertiesTrackingConfig(this);
900+
trackingOptions = Object.keys(trackingOptions).length > 0 ? {trackingOptions: trackingOptions} : {};
901+
apiProperties = merge(trackingOptions, (apiProperties || {}));
900902
eventProperties = eventProperties || {};
901903
groups = groups || {};
902904
var event = {
@@ -951,6 +953,19 @@ var _shouldTrackField = function _shouldTrackField(scope, field) {
951953
return !!scope.options.trackingOptions[field];
952954
};
953955

956+
var _generateApiPropertiesTrackingConfig = function _generateApiPropertiesTrackingConfig(scope) {
957+
// to limit size of config payload, only send fields that have been disabled
958+
var fields = ['city', 'country', 'dma', 'ip_address', 'region'];
959+
var config = {};
960+
for (var i = 0; i < fields.length; i++) {
961+
var field = fields[i];
962+
if (!_shouldTrackField(scope, field)) {
963+
config[field] = false;
964+
}
965+
}
966+
return config;
967+
};
968+
954969
/**
955970
* Remove old events from the beginning of the array if too many have accumulated. Default limit is 1000 events.
956971
* @private

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default {
2424
sessionTimeout: 30 * 60 * 1000,
2525
trackingOptions: {
2626
city: true,
27+
country: true,
2728
device_model: true,
2829
dma: true,
2930
ip_address: true,

test/amplitude-client.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ it ('should load saved events from localStorage new keys and send events', funct
724724
// check config loaded correctly
725725
assert.deepEqual(amplitude2.options.trackingOptions, {
726726
city: false,
727+
country: true,
727728
device_model: true,
728729
dma: true,
729730
ip_address: false,
@@ -1150,6 +1151,7 @@ describe('setVersionName', function() {
11501151
clock = sinon.useFakeTimers();
11511152
var trackingOptions = {
11521153
city: false,
1154+
country: true,
11531155
ip_address: false,
11541156
language: false,
11551157
platform: false,
@@ -1165,12 +1167,27 @@ describe('setVersionName', function() {
11651167

11661168
it('should not track language or platform', function() {
11671169
assert.equal(amplitude.options.trackingOptions.language, false);
1170+
assert.equal(amplitude.options.trackingOptions.platform, false);
11681171
amplitude.logEvent('Event Type 1');
11691172
assert.lengthOf(server.requests, 1);
11701173
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
11711174
assert.equal(events[0].language, null);
11721175
assert.equal(events[0].platform, null);
11731176
});
1177+
1178+
it('should send trackingOptions in api properties', function() {
1179+
amplitude.logEvent('Event Type 2');
1180+
assert.lengthOf(server.requests, 1);
1181+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1182+
1183+
// verify country is not sent since it matches the default value of true
1184+
assert.deepEqual(events[0].api_properties, {
1185+
trackingOptions: {
1186+
city: false,
1187+
ip_address: false,
1188+
}
1189+
});
1190+
});
11741191
});
11751192

11761193
describe('logEvent', function() {
@@ -1247,6 +1264,14 @@ describe('setVersionName', function() {
12471264
assert.isNotNull(events[0].language);
12481265
});
12491266

1267+
it('should not send trackingOptions in api properties', function() {
1268+
amplitude.logEvent('Event Should Not Send Tracking Properties');
1269+
assert.lengthOf(server.requests, 1);
1270+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
1271+
assert.equal(events.length, 1);
1272+
assert.deepEqual(events[0].api_properties, {});
1273+
});
1274+
12501275
it('should send platform', function() {
12511276
amplitude.logEvent('Event Should Send Platform');
12521277
assert.lengthOf(server.requests, 1);

0 commit comments

Comments
 (0)