2021-08-19 12:27:01 +00:00
|
|
|
import { jitsuClient, JitsuClient } from "@jitsu/sdk-js";
|
2021-09-22 19:52:38 +00:00
|
|
|
import React, { useContext } from "react";
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2021-05-07 17:05:33 +00:00
|
|
|
/**
|
|
|
|
* Enumeration of all event types that are being sent
|
|
|
|
* to telemetry collection.
|
|
|
|
*/
|
|
|
|
export const telemetryEventTypes = {
|
2021-08-19 12:27:01 +00:00
|
|
|
pageView: "page_view",
|
|
|
|
bookingConfirmed: "booking_confirmed",
|
|
|
|
bookingCancelled: "booking_cancelled",
|
|
|
|
};
|
2021-04-27 14:19:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Telemetry client
|
|
|
|
*/
|
|
|
|
export type TelemetryClient = {
|
2021-08-19 12:27:01 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const emptyClient: TelemetryClient = {
|
|
|
|
withJitsu: () => {
|
|
|
|
// empty
|
|
|
|
},
|
|
|
|
};
|
2021-04-27 14:19:12 +00:00
|
|
|
|
|
|
|
function useTelemetry(): TelemetryClient {
|
2021-08-19 12:27:01 +00:00
|
|
|
return useContext(TelemetryContext);
|
2021-04-27 14:19:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 17:05:33 +00:00
|
|
|
function isLocalhost(host: string) {
|
2021-08-19 12:27:01 +00:00
|
|
|
return "localhost" === host || "127.0.0.1" === host;
|
2021-05-07 17:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2021-08-19 12:27:01 +00:00
|
|
|
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: "",
|
|
|
|
};
|
2021-05-07 17:05:33 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 14:19:12 +00:00
|
|
|
function createTelemetryClient(): TelemetryClient {
|
2021-08-19 12:27:01 +00:00
|
|
|
if (process.env.NEXT_PUBLIC_TELEMETRY_KEY) {
|
|
|
|
return {
|
|
|
|
withJitsu: (callback) => {
|
|
|
|
if (!process.env.NEXT_PUBLIC_TELEMETRY_KEY) {
|
|
|
|
//telemetry is disabled
|
|
|
|
return;
|
2021-04-27 14:19:12 +00:00
|
|
|
}
|
2021-08-19 12:27:01 +00:00
|
|
|
if (!window) {
|
|
|
|
console.warn("Jitsu has been called during SSR, this scenario isn't supported yet");
|
|
|
|
return;
|
|
|
|
} else if (!window["jitsu"]) {
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2021-04-27 14:19:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const TelemetryContext = React.createContext<TelemetryClient>(emptyClient);
|
2021-04-27 14:19:12 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const TelemetryProvider = TelemetryContext.Provider;
|
2021-04-27 14:19:12 +00:00
|
|
|
|
|
|
|
export { TelemetryContext, TelemetryProvider, createTelemetryClient, useTelemetry };
|