Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force session recording to use lz64 #134

Merged
merged 2 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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