From ed81ce9d2180bb121232f0b89eec3c788ab281b7 Mon Sep 17 00:00:00 2001 From: Karl-Aksel Puulmann Date: Fri, 27 Nov 2020 13:25:14 +0200 Subject: [PATCH 1/2] Force lz64 compression for session recording The largest session recording event is the initial FULL_SNAPSHOT which might happen before initial /decide response. By forcing compression we make sure events reach us better. --- src/__tests__/extensions/sessionrecording.js | 2 ++ src/extensions/sessionrecording.js | 1 + src/posthog-core.js | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/__tests__/extensions/sessionrecording.js b/src/__tests__/extensions/sessionrecording.js index fd95e49d6..a6218987f 100644 --- a/src/__tests__/extensions/sessionrecording.js +++ b/src/__tests__/extensions/sessionrecording.js @@ -95,6 +95,7 @@ describe('SessionRecording', () => { method: 'POST', transport: 'XHR', endpoint: '/e/', + compression: 'lz64', _noTruncate: true, _batchKey: 'sessionRecording', } @@ -109,6 +110,7 @@ describe('SessionRecording', () => { method: 'POST', transport: 'XHR', endpoint: '/e/', + compression: 'lz64', _noTruncate: true, _batchKey: 'sessionRecording', } diff --git a/src/extensions/sessionrecording.js b/src/extensions/sessionrecording.js index eface4f37..3b45a2d90 100644 --- a/src/extensions/sessionrecording.js +++ b/src/extensions/sessionrecording.js @@ -82,6 +82,7 @@ export class SessionRecording { transport: 'XHR', method: 'POST', endpoint: this.endpoint, + compression: 'lz64', // Force lz64 even if /decide endpoint has not yet responded _noTruncate: true, _batchKey: 'sessionRecording', }) diff --git a/src/posthog-core.js b/src/posthog-core.js index 2a9b1c849..15f25e806 100644 --- a/src/posthog-core.js +++ b/src/posthog-core.js @@ -329,7 +329,7 @@ PostHogLib.prototype._handle_queued_event = function (url, data, options) { } PostHogLib.prototype.__compress_and_send_json_request = function (url, jsonData, options, callback) { - if (this.compression['lz64']) { + if (this.compression['lz64'] || (options.compression && options.compression === 'lz64')) { this._send_request(url, { data: LZString.compressToBase64(jsonData), compression: 'lz64' }, options, callback) } else { this._send_request(url, { data: _.base64Encode(jsonData) }, options, callback) From 80624cae7b428a8eeccb3ab295739870a4b30807 Mon Sep 17 00:00:00 2001 From: Karl-Aksel Puulmann Date: Fri, 27 Nov 2020 13:54:21 +0200 Subject: [PATCH 2/2] Send library version, compression as headers, test payload --- cypress/integration/capture.spec.js | 25 +++++++++++++++++++++++++ package.json | 2 +- src/posthog-core.js | 3 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cypress/integration/capture.spec.js b/cypress/integration/capture.spec.js index eaa966a53..986bdcce7 100644 --- a/cypress/integration/capture.spec.js +++ b/cypress/integration/capture.spec.js @@ -1,5 +1,8 @@ /// +import { version } from '../../package.json' +import { LZString } from '../../src/lz-string' + describe('Event capture', () => { given('options', () => ({})) given('sessionRecording', () => false) @@ -118,4 +121,26 @@ describe('Event capture', () => { cy.phCaptures().should('deep.equal', []) }) }) + + describe('decoding the payload', () => { + it('contains the correct headers and payload after an event', () => { + start() + + cy.get('[data-cy-custom-event-button]').click() + cy.phCaptures().should('deep.equal', ['$pageview', '$autocapture', 'custom-event']) + + cy.wait('@capture').its('request.headers').should('deep.equal', { + 'Content-Type': 'application/x-www-form-urlencoded', + PosthogJs: version, + PosthogCompression: 'lz64', + }) + + cy.get('@capture').should(({ request }) => { + const data = decodeURIComponent(request.body.match(/data=(.*)&compression=lz64/)[1]) + const captures = JSON.parse(LZString.decompressFromBase64(data)) + + expect(captures.map(({ event }) => event)).to.deep.equal(['$pageview', '$autocapture', 'custom-event']) + }) + }) + }) }) diff --git a/package.json b/package.json index 03fcae060..13d6fc8c9 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@typescript-eslint/parser": "^3.5.0", "babel-eslint": "^10.1.0", "babel-jest": "^26.1.0", - "cypress": "^5.5.0", + "cypress": "^6.0.0", "eslint": "^7.3.1", "eslint-plugin-prettier": "^3.1.4", "given2": "^2.1.7", diff --git a/src/posthog-core.js b/src/posthog-core.js index 15f25e806..bc5cf1a1f 100644 --- a/src/posthog-core.js +++ b/src/posthog-core.js @@ -391,6 +391,7 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) { var args = {} args['ip'] = this.get_config('ip') ? 1 : 0 args['_'] = new Date().getTime().toString() + const compression = data['compression'] || 'base64' if (use_post) { if (Array.isArray(data)) { @@ -426,6 +427,8 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) { var headers = this.get_config('xhr_headers') if (use_post) { headers['Content-Type'] = 'application/x-www-form-urlencoded' + headers['PosthogJs'] = Config.LIB_VERSION + headers['PosthogCompression'] = compression } _.each(headers, function (headerValue, headerName) { req.setRequestHeader(headerName, headerValue)