Skip to content

Commit 1621f4f

Browse files
authored
Merge pull request #93 from amplitude/https_flag
Add forceHttps flag
2 parents fbceb5a + babf3ba commit 1621f4f

File tree

8 files changed

+48
-5
lines changed

8 files changed

+48
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
* Add configuration option `forceHttps`, which when set to `true` forces the SDK to always upload to HTTPS endpoint. By default the SDK uses the endpoint that matches the embedding site's protocol (for example if your site is HTTP, it will use the HTTP endpoint).
4+
35
### 3.0.2 (July 6, 2016)
46

57
* `productId` is no longer a required field for `Revenue` logged via `logRevenueV2`.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ amplitude.getInstance().init('YOUR_API_KEY_HERE', null, {
327327
| domain | string | Custom cookie domain | The top domain of the current page's url |
328328
| eventUploadPeriodMillis | number | Amount of time in milliseconds that the SDK waits before uploading events if `batchEvents` is `true`. | 30\*1000 (30 sec) |
329329
| eventUploadThreshold | number | Minimum number of events to batch together per request if `batchEvents` is `true`. | 30 |
330+
| forceHttps | boolean | If `true`, the events will always be uploaded to HTTPS endpoint. Otherwise it will use the embedding site's protocol. | `false` |
330331
| includeReferrer | boolean | If `true`, captures the `referrer` and `referring_domain` for each session, as well as the user's `initial_referrer` and `initial_referring_domain` via a set once operation. | `false` |
331332
| includeUtm | boolean | If `true`, finds utm parameters in the query string or the __utmz cookie, parses, and includes them as user propeties on all events uploaded. Also captures initial utm parameters for each session via a set once operation. | `false` |
332333
| language | string | Custom language to set | Language determined by browser |

amplitude.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,8 @@ AmplitudeClient.prototype.sendEvents = function sendEvents(callback) {
14781478
}
14791479

14801480
this._sending = true;
1481-
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' + this.options.apiEndpoint + '/';
1481+
var protocol = this.options.forceHttps ? 'https' : ('https:' === window.location.protocol ? 'https' : 'http');
1482+
var url = protocol + '://' + this.options.apiEndpoint + '/';
14821483

14831484
// fetch events to send
14841485
var numEvents = Math.min(this._unsentCount(), this.options.uploadBatchSize);
@@ -4935,6 +4936,7 @@ module.exports = {
49354936
batchEvents: false,
49364937
eventUploadThreshold: 30,
49374938
eventUploadPeriodMillis: 30 * 1000, // 30s
4939+
forceHttps: false,
49384940
};
49394941

49404942
}, {"./language":29}],

amplitude.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude-client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,8 @@ AmplitudeClient.prototype.sendEvents = function sendEvents(callback) {
997997
}
998998

999999
this._sending = true;
1000-
var url = ('https:' === window.location.protocol ? 'https' : 'http') + '://' + this.options.apiEndpoint + '/';
1000+
var protocol = this.options.forceHttps ? 'https' : ('https:' === window.location.protocol ? 'https' : 'http');
1001+
var url = protocol + '://' + this.options.apiEndpoint + '/';
10011002

10021003
// fetch events to send
10031004
var numEvents = Math.min(this._unsentCount(), this.options.uploadBatchSize);

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ module.exports = {
2020
batchEvents: false,
2121
eventUploadThreshold: 30,
2222
eventUploadPeriodMillis: 30 * 1000, // 30s
23+
forceHttps: false,
2324
};

test/amplitude-client.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,24 @@ describe('setVersionName', function() {
10381038
assert.equal(server.requests[0].async, true);
10391039
});
10401040

1041+
it('should send https request', function() {
1042+
amplitude.options.forceHttps = true;
1043+
amplitude.logEvent('Event Type 1');
1044+
assert.lengthOf(server.requests, 1);
1045+
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1046+
assert.equal(server.requests[0].method, 'POST');
1047+
assert.equal(server.requests[0].async, true);
1048+
});
1049+
1050+
it('should send https request by configuration', function() {
1051+
amplitude.init(apiKey, null, { forceHttps: true });
1052+
amplitude.logEvent('Event Type 1');
1053+
assert.lengthOf(server.requests, 1);
1054+
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1055+
assert.equal(server.requests[0].method, 'POST');
1056+
assert.equal(server.requests[0].async, true);
1057+
});
1058+
10411059
it('should reject empty event types', function() {
10421060
amplitude.logEvent();
10431061
assert.lengthOf(server.requests, 0);

test/amplitude.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,24 @@ describe('setVersionName', function() {
10631063
assert.equal(server.requests[0].async, true);
10641064
});
10651065

1066+
it('should send https request', function() {
1067+
amplitude.options.forceHttps = true;
1068+
amplitude.logEvent('Event Type 1');
1069+
assert.lengthOf(server.requests, 1);
1070+
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1071+
assert.equal(server.requests[0].method, 'POST');
1072+
assert.equal(server.requests[0].async, true);
1073+
});
1074+
1075+
it('should send https request by configuration', function() {
1076+
amplitude.init(apiKey, null, { forceHttps: true });
1077+
amplitude.logEvent('Event Type 1');
1078+
assert.lengthOf(server.requests, 1);
1079+
assert.equal(server.requests[0].url, 'https://api.amplitude.com/');
1080+
assert.equal(server.requests[0].method, 'POST');
1081+
assert.equal(server.requests[0].async, true);
1082+
});
1083+
10661084
it('should reject empty event types', function() {
10671085
amplitude.logEvent();
10681086
assert.lengthOf(server.requests, 0);

0 commit comments

Comments
 (0)