Merge pull request #298 from appwrite/feat-environment-service

feat: system service
This commit is contained in:
Torsten Dittmann
2023-02-14 18:11:31 +01:00
committed by GitHub
6 changed files with 36 additions and 23 deletions
+8 -10
View File
@@ -3,16 +3,14 @@ import googleAnalytics from '@analytics/google-analytics';
import { get } from 'svelte/store';
import { page } from '$app/stores';
import { user } from '$lib/stores/user';
import { growthEndpoint, Mode } from '$lib/constants';
import { AppwriteException } from '@aw-labs/appwrite-console';
import { ENV, MODE, VARS } from '$lib/system';
const isDevelopment =
import.meta.env.DEV || import.meta.env?.VITE_VERCEL_ENV?.toString() === 'preview';
const analytics = Analytics({
app: 'appwrite',
plugins: [
googleAnalytics({
measurementIds: [import.meta.env.VITE_GA_PROJECT?.toString() || 'G-R4YJ9JN8L4']
measurementIds: [VARS.GOOGLE_ANALYTICS ?? 'G-R4YJ9JN8L4']
})
]
});
@@ -32,7 +30,7 @@ export function trackEvent(name: string, data: object = null): void {
};
}
if (isDevelopment) {
if (ENV.DEV || ENV.PREVIEW) {
console.debug(`[Analytics] Event ${name} ${path}`, data);
} else {
analytics.track(name, { ...data, path });
@@ -54,7 +52,7 @@ export function trackPageView(path: string) {
return;
}
if (isDevelopment) {
if (ENV.DEV || ENV.PREVIEW) {
console.debug(`[Analytics] Pageview ${path}`);
} else {
analytics.page({
@@ -64,14 +62,14 @@ export function trackPageView(path: string) {
}
function sendEventToGrowth(event: string, path: string, data: object = null): void {
if (!growthEndpoint) return;
if (!VARS.GROWTH_ENDPOINT) return;
const userStore = get(user);
let email: string, name: string;
if (userStore) {
email = userStore.email;
name = userStore.name;
}
fetch(`${growthEndpoint}/analytics`, {
fetch(`${VARS.GROWTH_ENDPOINT}/analytics`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@@ -80,7 +78,7 @@ function sendEventToGrowth(event: string, path: string, data: object = null): vo
action: event,
label: event,
url: window.location.origin + path,
account: import.meta.env.VITE_CONSOLE_MODE?.toString() || Mode.SELF_HOSTED,
account: MODE,
data: {
email,
name,
@@ -91,7 +89,7 @@ function sendEventToGrowth(event: string, path: string, data: object = null): vo
}
function isTrackingAllowed() {
if (import.meta.env?.VITEST) {
if (ENV.TEST) {
return;
}
if (window.navigator?.doNotTrack) {
-1
View File
@@ -20,7 +20,6 @@
async function handleSubmit() {
try {
await feedback.submitFeedback('feedback-nps', message, name, email, value);
console.log(value, message);
feedback.switchType('general');
} catch (error) {
feedback.switchType('general');
-7
View File
@@ -2,13 +2,6 @@ export const PAGE_LIMIT = 12; // default page limit
export const CARD_LIMIT = 6; // default card limit
export const INTERVAL = 5 * 60000; // default interval to check for feedback
export enum Mode {
CLOUD = 'cloud',
SELF_HOSTED = 'self-hosted'
}
export const growthEndpoint = import.meta.env.VITE_APPWRITE_GROWTH_ENDPOINT;
export enum Dependencies {
ORGANIZATION = 'dependency:organization',
PROJECT = 'dependency:project',
+3 -3
View File
@@ -1,5 +1,5 @@
import { browser } from '$app/environment';
import { growthEndpoint } from '$lib/constants';
import { VARS } from '$lib/system';
import { writable } from 'svelte/store';
export type AppStore = {
@@ -60,8 +60,8 @@ function createFeedbackStore() {
email?: string,
value?: number
) => {
if (!growthEndpoint) return;
const response = await fetch(`${growthEndpoint}/feedback`, {
if (!VARS.GROWTH_ENDPOINT) return;
const response = await fetch(`${VARS.GROWTH_ENDPOINT}/feedback`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
+2 -2
View File
@@ -1,3 +1,4 @@
import { VARS } from '$lib/system';
import {
Account,
Avatars,
@@ -12,8 +13,7 @@ import {
Users
} from '@aw-labs/appwrite-console';
const endpoint =
import.meta.env.VITE_APPWRITE_ENDPOINT?.toString() ?? `${globalThis?.location?.origin}/v1`;
const endpoint = VARS.APPWRITE_ENDPOINT ?? `${globalThis?.location?.origin}/v1`;
const clientConsole = new Client();
clientConsole.setEndpoint(endpoint).setProject('console');
+23
View File
@@ -0,0 +1,23 @@
export enum Mode {
CLOUD = 'cloud',
SELF_HOSTED = 'self-hosted'
}
export const VARS = {
APPWRITE_ENDPOINT: import.meta.env?.VITE_APPWRITE_ENDPOINT?.toString() as string | undefined,
GROWTH_ENDPOINT: import.meta.env?.VITE_APPWRITE_GROWTH_ENDPOINT?.toString() as
| string
| undefined,
CONSOLE_MODE: import.meta.env?.VITE_CONSOLE_MODE?.toString() as string | undefined,
VERCEL_ENV: import.meta.env?.VITE_VERCEL_ENV?.toString() as string | undefined,
GOOGLE_ANALYTICS: import.meta.env?.VITE_GA_PROJECT?.toString() as string | undefined
};
export const ENV = {
DEV: import.meta.env.DEV,
PROD: import.meta.env.PROD,
PREVIEW: VARS.VERCEL_ENV === 'preview',
TEST: !!import.meta.env?.VITEST
};
export const MODE = VARS.CONSOLE_MODE === Mode.CLOUD ? Mode.CLOUD : Mode.SELF_HOSTED;