Skip to content

Commit

Permalink
Merge pull request #298 from appwrite/feat-environment-service
Browse files Browse the repository at this point in the history
feat: system service
  • Loading branch information
TorstenDittmann authored Feb 14, 2023
2 parents fcf9ecb + 0950c86 commit 5be81fc
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
18 changes: 8 additions & 10 deletions src/lib/actions/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
})
]
});
Expand All @@ -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 });
Expand All @@ -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({
Expand All @@ -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'
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/lib/components/feedbackNPS.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 0 additions & 7 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions src/lib/stores/app.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/lib/stores/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { VARS } from '$lib/system';
import {
Account,
Avatars,
Expand All @@ -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');

Expand Down
23 changes: 23 additions & 0 deletions src/lib/system.ts
Original file line number Diff line number Diff line change
@@ -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;

1 comment on commit 5be81fc

@vercel
Copy link

@vercel vercel bot commented on 5be81fc Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.