1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/extensions/survey/src/survey.ts
Lauri Nevala a0e24e0a4d
In-app survey extension (#1945)
* 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>
2021-02-04 18:22:22 +02:00

47 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>();