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

ui: Release notes signup - Add Custom Analytics actions and sagas #44821

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
50 changes: 50 additions & 0 deletions pkg/ui/src/redux/customAnalytics/customAnalyticsSagas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

import { expectSaga } from "redux-saga-test-plan";
import sinon from "sinon";
import Analytics from "analytics-node";

import { signUpEmailSubscription } from "./customAnalyticsSagas";
import {
completeEmailSubscriptionSignUp,
signUpForEmailSubscription,
} from "./customAnanlyticsActions";

const sandbox = sinon.createSandbox();

describe("customAnalyticsSagas", () => {
describe("signUpEmailSubscription generator", () => {
afterEach(() => {
sandbox.reset();
});

it("calls analytics#identify with user email in args ", () => {
const analyticsIdentifyFn = sandbox.stub(Analytics.prototype, "identify");
const clusterId = "cluster-1";
const email = "foo@bar.com";
const action = signUpForEmailSubscription(clusterId, email);

return expectSaga(signUpEmailSubscription, action)
.put(completeEmailSubscriptionSignUp())
.dispatch(action)
.run()
.then(() => {
const expectedAnalyticsMessage = {
userId: clusterId,
traits: {
email,
},
};
analyticsIdentifyFn.calledOnceWith(expectedAnalyticsMessage);
});
});
});
});
54 changes: 54 additions & 0 deletions pkg/ui/src/redux/customAnalytics/customAnalyticsSagas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

import { call, put, takeEvery } from "redux-saga/effects";
import Analytics from "analytics-node";

import { PayloadAction } from "src/interfaces/action";
import { COCKROACHLABS_ADDR } from "src/util/cockroachlabsAPI";
import {
completeEmailSubscriptionSignUp,
EMAIL_SUBSCRIPTION_SIGN_UP,
EmailSubscriptionSignUpPayload,
} from "./customAnanlyticsActions";

export type AnalyticsClientTarget = "email_sign_up";

// TODO (koorosh): has to be moved out from code base
const EMAIL_SIGN_UP_CLIENT_KEY = "72EEC0nqQKfoLWq0ZcGoTkJFIG9G9SII";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, we'll just leave this in-line since we do the same in analytics.ts, but good to leave the TODO.


const analyticsOpts = {
host: COCKROACHLABS_ADDR + "/api/segment",
};

export function getAnalyticsClientFor(target: AnalyticsClientTarget): Analytics {
switch (target) {
case "email_sign_up":
return new Analytics(EMAIL_SIGN_UP_CLIENT_KEY, analyticsOpts);
default:
throw new Error("Unrecognized Analytics Client target.");
}
}

export function* signUpEmailSubscription(action: PayloadAction<EmailSubscriptionSignUpPayload>) {
const client = getAnalyticsClientFor("email_sign_up");
const { clusterId, email } = action.payload;
yield call([client, client.identify], {
userId: clusterId,
traits: {
email,
},
});
yield put(completeEmailSubscriptionSignUp());
}

export function* customAnalyticsSaga() {
yield takeEvery(EMAIL_SUBSCRIPTION_SIGN_UP, signUpEmailSubscription);
}
37 changes: 37 additions & 0 deletions pkg/ui/src/redux/customAnalytics/customAnanlyticsActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

import { Action } from "redux";

import { PayloadAction } from "src/interfaces/action";

export const EMAIL_SUBSCRIPTION_SIGN_UP = "cockroachui/customanalytics/EMAIL_SUBSCRIPTION_SIGN_UP";
export const EMAIL_SUBSCRIPTION_SIGN_UP_COMPLETE = "cockroachui/customanalytics/EMAIL_SUBSCRIPTION_SIGN_UP_COMPLETE";

export type EmailSubscriptionSignUpPayload = {
email: string;
clusterId: string;
};

export function signUpForEmailSubscription(clusterId: string, email: string): PayloadAction<EmailSubscriptionSignUpPayload> {
return {
type: EMAIL_SUBSCRIPTION_SIGN_UP,
payload: {
email,
clusterId,
},
};
}

export function completeEmailSubscriptionSignUp(): Action {
return {
type: EMAIL_SUBSCRIPTION_SIGN_UP_COMPLETE,
};
}
12 changes: 12 additions & 0 deletions pkg/ui/src/redux/customAnalytics/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

export * from "./customAnalyticsSagas";
export * from "./customAnanlyticsActions";
2 changes: 2 additions & 0 deletions pkg/ui/src/redux/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { all, fork } from "redux-saga/effects";

import { queryMetricsSaga } from "./metrics";
import { localSettingsSaga } from "./localsettings";
import { customAnalyticsSaga } from "./customAnalytics";

export default function* rootSaga() {
yield all([
fork(queryMetricsSaga),
fork(localSettingsSaga),
fork(customAnalyticsSaga),
]);
}