Skip to content

Commit

Permalink
feat(analytics): add timestamp to every event sent
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Jun 29, 2021
1 parent 48c6857 commit 550942d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 7 deletions.
8 changes: 5 additions & 3 deletions docs/misc/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ List of available configured endpoints:

Example URI:

`https://data.trezor.io/suite/log/web/stable.log?c_v=1.8&c_type=transport-type&c_commit=4d09d88476dab2e6b2fbfb833b749e9ac62251c2&c_instance_id=qlT0xL2XKV&c_session_id=FZjilOYQic&type=bridge&version=2.0.30`
`https://data.trezor.io/suite/log/web/stable.log?c_v=1.8&c_type=transport-type&c_commit=4d09d88476dab2e6b2fbfb833b749e9ac62251c2&c_instance_id=qlT0xL2XKV&c_session_id=FZjilOYQic&c_timestamp=1624893047903&type=bridge&version=2.0.30`

Which tracks:
```
{
c_v: '1.9',
c_v: '1.11',
c_type: 'transport-type',
c_commit: '4d09d88476dab2e6b2fbfb833b749e9ac62251c2',
c_instance_id: 'qlT0xL2XKV',
c_session_id: 'FZjilOYQic',
c_timestamp: 1624893047903,
type: 'bridge',
version: '2.0.30'
}
Expand All @@ -62,6 +63,7 @@ Attributes which are always tracked:
- **c_commit**: current revision of app
- **c_instance_id**: until user does not wipe storage, the id is still same
- **c_session_id**: id changed on every launch of app
- **c_timestamp**: time in ms when event is created

Other attributes are connected to a specific type of events.

Expand Down Expand Up @@ -90,6 +92,7 @@ Breaking change should bump major version. Any other change bumps minor version.
## Changelog
### 1.11
Added:
- c_timestamp: number (time of created in ms sent with every event)
- menu/settings/dropdown
- option: 'guide' (+ old ones)
- menu/guide
Expand All @@ -102,7 +105,6 @@ Added:
- guide/report
- type: 'overview' | 'bug' | 'suggestion'


### 1.10
Removed:
- initial-run-completed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { urlSearchParams } from '../../../../../suite/src/utils/suite/metadata';
type Requests = ReturnType<typeof urlSearchParams>[];
const requests: Requests = [];
const instance = new RegExp(/^[A-Za-z0-9]{10,10}$/);
const timestamp = new RegExp(/^[0-9]{13,16}$/);

describe('Analytics', () => {
beforeEach(() => {
Expand Down Expand Up @@ -42,6 +43,11 @@ describe('Analytics', () => {
cy.wait('@data-fetch');
cy.wrap(requests).its(0).its('c_session_id').as('request0');
cy.wrap(requests).its(0).should('have.property', 'c_type', 'initial-run-completed');
cy.wrap(requests)
.its(0)
.should('have.property', 'c_timestamp')
.should('match', timestamp)
.as('timestamp0');
cy.wrap(requests).its(0).should('have.property', 'analytics', 'false');
cy.wrap(requests).its(1).should('equal', undefined);

Expand All @@ -68,6 +74,17 @@ describe('Analytics', () => {
cy.getTestElement('@analytics/toggle-switch').should('be.checked');
cy.wait('@data-fetch');
cy.wrap(requests).its(1).should('have.property', 'c_type', 'analytics/enable');
cy.wrap(requests)
.its(1)
.should('have.property', 'c_timestamp')
.should('match', timestamp)
.as('timestamp1');
// check that timestamp changes between requests
cy.get('@timestamp0').then(t0 => {
cy.get('@timestamp1').then(t1 => {
expect(t1).not.to.equal(t0);
});
});

// change fiat
cy.getTestElement('@settings/fiat-select/input').click();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ describe('Analytics Actions', () => {
});

it('analyticsActions.report() - should report if enabled (desktop)', () => {
const timestamp = new Date().getTime();
jest.spyOn(Date, 'now').mockImplementation(() => timestamp);

const env = process.env.SUITE_TYPE;
process.env.SUITE_TYPE = 'desktop';
const state = getInitialState({
Expand All @@ -83,13 +86,16 @@ describe('Analytics Actions', () => {
store.dispatch(analyticsActions.report({ type: 'switch-device/eject' }));
expect(global.fetch).toHaveBeenNthCalledWith(
1,
`https://data.trezor.io/suite/log/desktop/develop.log?c_v=${analyticsActions.version}&c_type=switch-device%2Feject&c_instance_id=1&c_session_id=very-random`,
`https://data.trezor.io/suite/log/desktop/develop.log?c_v=${analyticsActions.version}&c_type=switch-device%2Feject&c_instance_id=1&c_session_id=very-random&c_timestamp=${timestamp}`,
{ method: 'GET' },
);
process.env.SUITE_TYPE = env;
});

it('analyticsActions.report() - should report if enabled (web)', () => {
const timestamp = new Date().getTime();
jest.spyOn(Date, 'now').mockImplementation(() => timestamp);

const env = process.env.SUITE_TYPE;
process.env.SUITE_TYPE = 'web';
const state = getInitialState({
Expand All @@ -99,7 +105,7 @@ describe('Analytics Actions', () => {
store.dispatch(analyticsActions.report({ type: 'switch-device/eject' }));
expect(global.fetch).toHaveBeenNthCalledWith(
1,
`https://data.trezor.io/suite/log/web/develop.log?c_v=${analyticsActions.version}&c_type=switch-device%2Feject&c_instance_id=1&c_session_id=very-random`,
`https://data.trezor.io/suite/log/web/develop.log?c_v=${analyticsActions.version}&c_type=switch-device%2Feject&c_instance_id=1&c_session_id=very-random&c_timestamp=${timestamp}`,
{ method: 'GET' },
);
process.env.SUITE_TYPE = env;
Expand Down
10 changes: 8 additions & 2 deletions packages/suite/src/utils/suite/__fixtures__/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@ const version = '1.0';

export default [
{
currentDate: '2021-04-01T12:10:00.000Z',
input: {
type: 'transport-type',
payload: {
type: 'bridge',
version: '2',
},
},
encoded: `c_v=${version}&c_type=transport-type&c_instance_id=1&c_session_id=2&type=bridge&version=2`,
encoded: `c_v=${version}&c_type=transport-type&c_instance_id=1&c_session_id=2&c_timestamp=${new Date(
'2021-04-01T12:10:00.000Z',
).getTime()}&type=bridge&version=2`,
},
{
currentDate: '2021-04-01T12:10:00.000Z',
input: {
type: 'initial-run-completed',
payload: {
analytics: false,
},
},
encoded: `c_v=${version}&c_type=initial-run-completed&c_instance_id=1&c_session_id=2&analytics=false`,
encoded: `c_v=${version}&c_type=initial-run-completed&c_instance_id=1&c_session_id=2&c_timestamp=${new Date(
'2021-04-01T12:10:00.000Z',
).getTime()}&analytics=false`,
},
] as const;
2 changes: 2 additions & 0 deletions packages/suite/src/utils/suite/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fixtures from '../__fixtures__/analytics';
describe('analytics', () => {
fixtures.forEach(f => {
it(f.input.type, () => {
jest.spyOn(Date, 'now').mockImplementation(() => new Date(f.currentDate).getTime());

expect(
encodeDataToQueryString(f.input, {
instanceId: '1',
Expand Down
1 change: 1 addition & 0 deletions packages/suite/src/utils/suite/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const encodeDataToQueryString = (event: AnalyticsEvent, common: Common) =
c_commit: process.env.COMMITHASH,
c_instance_id: instanceId,
c_session_id: sessionId,
c_timestamp: Date.now(),
// eslint-enable @typescript-eslint/naming-convention
});

Expand Down

0 comments on commit 550942d

Please sign in to comment.