Skip to content

Commit

Permalink
Merge pull request #134 from PostHog/compression
Browse files Browse the repository at this point in the history
Force session recording to use lz64
  • Loading branch information
timgl authored Nov 27, 2020
2 parents 6e879fe + 80624ca commit 027fa8c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
25 changes: 25 additions & 0 deletions cypress/integration/capture.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// <reference types="cypress" />

import { version } from '../../package.json'
import { LZString } from '../../src/lz-string'

describe('Event capture', () => {
given('options', () => ({}))
given('sessionRecording', () => false)
Expand Down Expand Up @@ -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'])
})
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/extensions/sessionrecording.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ describe('SessionRecording', () => {
method: 'POST',
transport: 'XHR',
endpoint: '/e/',
compression: 'lz64',
_noTruncate: true,
_batchKey: 'sessionRecording',
}
Expand All @@ -109,6 +110,7 @@ describe('SessionRecording', () => {
method: 'POST',
transport: 'XHR',
endpoint: '/e/',
compression: 'lz64',
_noTruncate: true,
_batchKey: 'sessionRecording',
}
Expand Down
1 change: 1 addition & 0 deletions src/extensions/sessionrecording.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand Down
5 changes: 4 additions & 1 deletion src/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 027fa8c

Please sign in to comment.