github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/redux/customAnalytics/customAnalyticsSagas.ts (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  import { call, put, takeEvery } from "redux-saga/effects";
    12  import Analytics from "analytics-node";
    13  
    14  import { PayloadAction } from "src/interfaces/action";
    15  import { COCKROACHLABS_ADDR } from "src/util/cockroachlabsAPI";
    16  import { emailSubscriptionAlertLocalSetting } from "src/redux/alerts";
    17  
    18  import {
    19    EMAIL_SUBSCRIPTION_SIGN_UP,
    20    EmailSubscriptionSignUpPayload,
    21  } from "./customAnanlyticsActions";
    22  
    23  export type AnalyticsClientTarget = "email_sign_up";
    24  
    25  // TODO (koorosh): has to be moved out from code base
    26  const EMAIL_SIGN_UP_CLIENT_KEY = "72EEC0nqQKfoLWq0ZcGoTkJFIG9G9SII";
    27  
    28  const analyticsOpts = {
    29    host: COCKROACHLABS_ADDR + "/api/segment",
    30  };
    31  
    32  export function getAnalyticsClientFor(target: AnalyticsClientTarget): Analytics {
    33    switch (target) {
    34      case "email_sign_up":
    35        return new Analytics(EMAIL_SIGN_UP_CLIENT_KEY, analyticsOpts);
    36      default:
    37        throw new Error("Unrecognized Analytics Client target.");
    38    }
    39  }
    40  
    41  export function* signUpEmailSubscription(action: PayloadAction<EmailSubscriptionSignUpPayload>) {
    42    const client = getAnalyticsClientFor("email_sign_up");
    43    const { clusterId, email } = action.payload;
    44    yield call([client, client.identify], {
    45      userId: clusterId,
    46      traits: {
    47        email,
    48        release_notes_sign_up_from_admin_ui: "true",
    49        product_updates: "true",
    50      },
    51    });
    52    yield put(emailSubscriptionAlertLocalSetting.set(true));
    53  }
    54  
    55  export function* customAnalyticsSaga() {
    56    yield takeEvery(EMAIL_SUBSCRIPTION_SIGN_UP, signUpEmailSubscription);
    57  }