mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* Initial in-app survey implementation Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com> * Add surveyId fetching and store integration Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com> * Add empty line Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com> * Fix typos Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com> * Use async version of machineId + refactoring Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com> * Update preferences hint text Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com> Co-authored-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { Util } from "@k8slens/extensions";
|
||
import { machineId } from "node-machine-id";
|
||
import Refiner from "refiner-js";
|
||
import got from "got";
|
||
import { surveyPreferencesStore } from "./survey-preferences-store";
|
||
|
||
type SurveyIdResponse = {
|
||
surveyId: string;
|
||
};
|
||
export class Survey extends Util.Singleton {
|
||
static readonly PROJECT_ID = "af468d00-4f8f-11eb-b01d-23b6562fef43";
|
||
protected anonymousId: string;
|
||
|
||
private constructor() {
|
||
super();
|
||
}
|
||
|
||
async start() {
|
||
await surveyPreferencesStore.whenEnabled;
|
||
|
||
const surveyId = await this.fetchSurveyId();
|
||
|
||
if (surveyId) {
|
||
Refiner("setProject", Survey.PROJECT_ID);
|
||
Refiner("identifyUser", {
|
||
id: surveyId,
|
||
});
|
||
|
||
}
|
||
}
|
||
|
||
async fetchSurveyId() {
|
||
try {
|
||
const surveyApi = process.env.SURVEY_API_URL || "https://survey.k8slens.dev";
|
||
const anonymousId = await machineId();
|
||
const { body } = await got(`${surveyApi}/api/survey-id?anonymousId=${anonymousId}`, { responseType: "json"});
|
||
|
||
return (body as SurveyIdResponse).surveyId;
|
||
} catch(error) {
|
||
return null;
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
export const survey = Survey.getInstance<Survey>();
|