calcom/lib/telemetry.ts
Deepak Prabhakara 7b65942de2
Feature/sso signup (#1555)
* updated saml-jackson

* if logged in redirect to getting-started page with username in the query param

* fixed issue with mixed up Google login, profile.id is undefined and this is causing the first record to be retrieved instead of the AND query failing

* updated updated saml-jackson

* document PGSSLMODE for Heroku

* tweaks to PGSSLMODE doc

* for self-hosted instance just allow user to signin with any identity (as long as email matches)

* fixed submitting flag

* added username to onboarding flow (if requested during signup)

* added telemetry for google login, saml login, saml config

* check if firstName and lastName are defined

* convert mutation to an async op

* added e2e test to ensure username query param gets picked up during onboarding

* fixed minor typo and added note about configuring Google integration as an Internal app when self-hosting

* cleaned up unnecessary ssr in sso signup routes

* renamed function

* Revert "cleaned up unnecessary ssr in sso signup routes"

This reverts commit 3607ffef79542d8ca4277a64be38d35bd9457960.

* moved client side code to useEffect hook

* - format
- fixed Save button in SAML config component

Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-02-02 18:33:27 +00:00

114 lines
3.2 KiB
TypeScript

import { jitsuClient, JitsuClient } from "@jitsu/sdk-js";
import React, { useContext } from "react";
/**
* Enumeration of all event types that are being sent
* to telemetry collection.
*/
export const telemetryEventTypes = {
pageView: "page_view",
bookingConfirmed: "booking_confirmed",
bookingCancelled: "booking_cancelled",
importSubmitted: "import_submitted",
googleLogin: "google_login",
samlLogin: "saml_login",
samlConfig: "saml_config",
};
/**
* Telemetry client
*/
export type TelemetryClient = {
/**
* Use it as: withJitsu((jitsu) => {return jitsu.track()}). If telemetry is disabled, the callback will ignored
*
* ATTENTION: always return the value of jitsu.track() or id() call. Otherwise unhandled rejection can happen,
* which is handled in Next.js with a popup.
*/
withJitsu: (callback: (jitsu: JitsuClient) => void | Promise<void>) => void;
};
const emptyClient: TelemetryClient = {
withJitsu: () => {
// empty
},
};
function useTelemetry(): TelemetryClient {
return useContext(TelemetryContext);
}
function isLocalhost(host: string) {
return "localhost" === host || "127.0.0.1" === host;
}
/**
* Collects page parameters and makes sure no sensitive data made it to telemetry
* @param route current next.js route
*/
export function collectPageParameters(route?: string): any {
const host = document.location.hostname;
const maskedHost = isLocalhost(host) ? "localhost" : "masked";
//starts with ''
const docPath = route ?? "";
return {
page_url: route,
page_title: "",
source_ip: "",
url: document.location.protocol + "//" + host + (docPath ?? ""),
doc_host: maskedHost,
doc_search: "",
doc_path: docPath,
referer: "",
};
}
function createTelemetryClient(): TelemetryClient {
if (process.env.NEXT_PUBLIC_TELEMETRY_KEY) {
return {
withJitsu: (callback) => {
if (!process.env.NEXT_PUBLIC_TELEMETRY_KEY) {
//telemetry is disabled
return;
}
if (!window) {
console.warn("Jitsu has been called during SSR, this scenario isn't supported yet");
return;
} else if (
// FIXME
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
!window["jitsu"]
) {
// FIXME
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window["jitsu"] = jitsuClient({
log_level: "ERROR",
tracking_host: "https://t.calendso.com",
key: process.env.NEXT_PUBLIC_TELEMETRY_KEY,
cookie_name: "__clnds",
capture_3rd_party_cookies: false,
});
}
// FIXME
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const res = callback(window["jitsu"]);
if (res && typeof res["catch"] === "function") {
res.catch((e) => {
console.debug("Unable to send telemetry event", e);
});
}
},
};
} else {
return emptyClient;
}
}
const TelemetryContext = React.createContext<TelemetryClient>(emptyClient);
const TelemetryProvider = TelemetryContext.Provider;
export { TelemetryContext, TelemetryProvider, createTelemetryClient, useTelemetry };