diff --git a/src/lib/sdk/sources.ts b/src/lib/sdk/sources.ts new file mode 100644 index 000000000..bf9f703c7 --- /dev/null +++ b/src/lib/sdk/sources.ts @@ -0,0 +1,45 @@ +import type { Client } from '@appwrite.io/console'; + +type Source = { + $id: string; + fingerprint: string; + referrer: string; + ref: string; + utmSource: string; + utmCampaign: string; + utmMedium: string; +}; + +export class Sources { + client: Client; + + constructor(client: Client) { + this.client = client; + } + + async create( + ref: string, + referrer: string, + utmSource: string, + utmCampaign: string, + utmMedium: string + ): Promise { + const path = `/console/sources`; + const params = { + ref, + referrer, + utmSource, + utmCampaign, + utmMedium + }; + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'POST', + uri, + { + 'content-type': 'application/json' + }, + params + ); + } +} diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index 649681e53..8dacdc079 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -22,6 +22,7 @@ import { Vcs } from '@appwrite.io/console'; import { Billing } from '../sdk/billing'; +import { Sources } from '$lib/sdk/sources'; const endpoint = VARS.APPWRITE_ENDPOINT ?? `${globalThis?.location?.origin}/v1`; @@ -72,7 +73,8 @@ export const sdk = { migrations: new Migrations(clientConsole), console: new Console(clientConsole), assistant: new Assistant(clientConsole), - billing: new Billing(clientConsole) + billing: new Billing(clientConsole), + sources: new Sources(clientConsole) }, get forProject() { const projectId = getProjectId(); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index e462b1860..1da2b87eb 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -13,9 +13,29 @@ import { loading, requestedMigration } from './store'; import { parseIfString } from '$lib/helpers/object'; import Consent from '$lib/components/consent.svelte'; + import { sdk } from '$lib/stores/sdk'; import type { Models } from '@appwrite.io/console'; onMount(async () => { + // handle sources + if (isCloud) { + const urlParams = $page.url.searchParams; + const ref = urlParams.get('ref'); + const utmSource = urlParams.get('utm_source'); + const utmMedium = urlParams.get('utm_medium'); + const utmCampaign = urlParams.get('utm_campaign'); + let referrer = document.referrer.length ? document.referrer : null; + + // Skip our own + if (referrer?.includes('//appwrite.io')) { + referrer = null; + } + + if (ref || referrer || utmSource || utmCampaign || utmMedium) { + sdk.forConsole.sources.create(ref, referrer, utmSource, utmCampaign, utmMedium); + } + } + if ($page.url.searchParams.has('migrate')) { const migrateData = $page.url.searchParams.get('migrate'); requestedMigration.set(parseIfString(migrateData) as Record);