Skip to content

Commit

Permalink
chore: track events (#2624)
Browse files Browse the repository at this point in the history
* chore: track events

* name fix

* event names

* name fix

* fix
  • Loading branch information
vasilev-alex committed Apr 30, 2021
1 parent 30d52c4 commit cd6fb93
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/cubejs-backend-shared/src/track.ts
Expand Up @@ -14,6 +14,7 @@ export type Event = BaseEvent & {
anonymousId: string,
platform: string,
nodeVersion: string,
sentFrom: 'backend';
};

let flushPromise: Promise<any>|null = null;
Expand Down Expand Up @@ -84,6 +85,7 @@ export async function track(opts: BaseEvent) {
arch: process.arch,
nodeVersion: process.version,
anonymousId,
sentFrom: 'backend'
});

const currentPromise = (flushPromise || Promise.resolve()).then(() => flush()).then(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-client-core/index.d.ts
Expand Up @@ -131,6 +131,7 @@ declare module '@cubejs-client/core' {
lastRefreshTime: string;
query: Query;
data: T[];
usedPreAggregations?: Record<string, any>;
};

export type LoadResponse<T> = {
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-playground/src/App.tsx
Expand Up @@ -55,6 +55,7 @@ class App extends Component<RouteComponentProps, TAppState> {
setAnonymousId(context.anonymousId, {
coreServerVersion: context.coreServerVersion,
projectFingerprint: context.projectFingerprint,
isDocker: Boolean(context.isDocker)
});
this.setState({ context }, () => {
if (context.shouldStartConnectionWizardFlow) {
Expand Down
Expand Up @@ -8,6 +8,7 @@ import type { PivotConfig, Query, ChartType } from '@cubejs-client/core';

import { Button, CubeLoader } from '../../atoms';
import { UIFramework } from '../../types';
import { event } from '../../events';

const { Text } = Typography;

Expand Down Expand Up @@ -139,6 +140,10 @@ export default function ChartRenderer({
setSlowQueryFromCache(Boolean(loadResponse.slowQuery));
Boolean(loadResponse.slowQuery) && setSlowQuery(false);
setResultSet(true);

const servedByPreAggregation = Object.keys(loadResponse.results[0]?.usedPreAggregations || {}).length > 0;

event(servedByPreAggregation ? 'load_request_success_aggregated:frontend' : 'load_request_success:frontend');
}

if (resultSet || error) {
Expand Down
28 changes: 17 additions & 11 deletions packages/cubejs-playground/src/events.ts
Expand Up @@ -3,36 +3,38 @@ import cookie from 'component-cookie';
import uuidv4 from 'uuid/v4';

let flushPromise = null;
let trackEvents: string[] = [];
let baseProps = {};
let trackEvents: BaseEvent[] = [];
let baseProps = {
sentFrom: 'frontend'
};

const track = async (event) => {
if (!cookie('playground_anonymous')) {
cookie('playground_anonymous', uuidv4());
}

trackEvents.push({
...baseProps,
...event,
id: uuidv4(),
clientAnonymousId: cookie('playground_anonymous'),
clientTimestamp: new Date().toJSON(),
});
const flush = async (toFlush?: string[], retries?: number) => {

const flush = async (toFlush?: BaseEvent[], retries: number = 10) => {
if (!toFlush) {
toFlush = trackEvents;
trackEvents = [];
}

if (!toFlush.length) {
return null;
}
if (retries == null) {
retries = 10;
}

try {
const sentAt = new Date().toJSON();
const result = await fetch('https://track.cube.dev/track', {
method: 'post',
// @ts-ignore
body: JSON.stringify(toFlush.map((r) => ({ ...r, sentAt }))),
headers: { 'Content-Type': 'application/json' },
});
Expand All @@ -43,7 +45,6 @@ const track = async (event) => {
if (retries > 0) {
return flush(toFlush, retries - 1);
}
// console.log(e);
}
return null;
};
Expand All @@ -60,15 +61,20 @@ const track = async (event) => {
};

export const setAnonymousId = (anonymousId, props) => {
baseProps = props;
baseProps = {
...baseProps,
...props
};
track({ event: 'identify', anonymousId, ...props });
};

export const event = (name, params) => {
type BaseEvent = Record<string, any>;

export const event = (name: string, params: BaseEvent = {}) => {
track({ event: name, ...params });
};

export const playgroundAction = (name, options = {}) => {
export const playgroundAction = (name: string, options: BaseEvent = {}) => {
event('Playground Action', { name, ...options });
};

Expand Down
Expand Up @@ -9,6 +9,7 @@ import { DatabaseCard, SelectedDatabaseCard } from './components/DatabaseCard';
import DatabaseForm from './components/DatabaseForm';
import { Button } from '../../atoms';
import { LocalhostTipBox } from './components/LocalhostTipBox';
import { event, playgroundAction } from '../../events';

const { Title, Paragraph } = Typography;

Expand Down Expand Up @@ -78,6 +79,10 @@ export default function ConnectionWizardPage({ history }) {
const [testConnectionResult, setTestConnectionResult] = useState<any>(null);
const [db, selectDatabase] = useState<Database | null>(null);

useEffect(() => {
playgroundAction('connection_wizard_open');
}, []);

useEffect(() => {
setTestConnectionLoading(false);
setTestConnectionResult(null);
Expand Down Expand Up @@ -145,12 +150,16 @@ export default function ConnectionWizardPage({ history }) {
await saveConnection(variables);
setLoading(false);

event('test_database_connection_success:frontend');

history.push('/schema');
} catch (error) {
setTestConnectionResult({
success: false,
error,
});

event('test_database_connection_error:frontend');
}

setTestConnectionLoading(false);
Expand Down
Expand Up @@ -54,7 +54,7 @@ context('Playground: Connection Wizard', () => {
cy.getByTestId('wizard-localhost-tipbox').should('exist');
});

it('fails to connect to the DB with wrong crednetials', () => {
it('fails to connect to the DB with wrong credentials', () => {
cy.visit('/');
cy.getByTestId('wizard-db-card').contains('PostgreSQL').click();
cy.fixture('databases.json').then(({ postgresql }) => {
Expand Down

0 comments on commit cd6fb93

Please sign in to comment.