Skip to content

Commit

Permalink
add umami analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixTJDietrich committed Sep 14, 2024
1 parent c793a27 commit e3fd4a0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
20 changes: 16 additions & 4 deletions webapp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@ RUN cat .env
RUN COOLIFY_URL_VALUE=$(grep '^COOLIFY_URL=' .env | cut -d '=' -f2) && \
sed -i "s|\$COOLIFY_URL|$COOLIFY_URL_VALUE|g" .env

# Set serverUrl in environment.ts
RUN APPLICATION_SERVER_URL=$(grep '^APPLICATION_SERVER_URL=' .env | cut -d '=' -f2) && \
echo "Replacing serverUrl in environment.ts with: $APPLICATION_SERVER_URL" && \
sed -i "s|serverUrl: '[^']*'|serverUrl: '$APPLICATION_SERVER_URL'|g" src/environments/environment.ts
# Export environment variables from .env
# This assumes that .env contains lines like VARIABLE=value
# and does not contain spaces around the '='
RUN export $(grep -v '^#' .env | xargs) && \
echo "Generating environment.prod.ts" && \
cat > src/environments/environment.prod.ts <<EOF
export const environment = {
serverUrl: '${APPLICATION_SERVER_URL}',
umami: {
enabled: ${UMAMI_ENABLED},
scriptUrl: '${UMAMI_SCRIPT_URL}',
websiteId: '${UMAMI_WEBSITE_ID}',
domains: '${UMAMI_DOMAINS}'
}
};
EOF

RUN npm install
RUN npm run build
Expand Down
6 changes: 6 additions & 0 deletions webapp/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"budgets": [
{
"type": "initial",
Expand Down
38 changes: 38 additions & 0 deletions webapp/src/app/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable } from '@angular/core';
import { environment } from 'environments/environment';

@Injectable({
providedIn: 'root'
})
export class AnalyticsService {
private scriptLoaded = false;

initialize(): void {
if (environment.umami.enabled) {
this.loadUmamiScript();
}
}

private loadUmamiScript(): void {
if (this.scriptLoaded) {
return;
}

const script = document.createElement('script');
script.defer = true;
script.src = environment.umami.scriptUrl;
script.setAttribute('data-website-id', environment.umami.websiteId);
script.setAttribute('data-domains', environment.umami.domains);

script.onload = () => {
console.log('Umami analytics script loaded successfully.');
};

script.onerror = () => {
console.error('Failed to load Umami analytics script.');
};

document.body.appendChild(script);
this.scriptLoaded = true;
}
}
12 changes: 10 additions & 2 deletions webapp/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApplicationConfig, importProvidersFrom, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { APP_INITIALIZER, ApplicationConfig, importProvidersFrom, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
Expand All @@ -7,6 +7,13 @@ import { LucideAngularModule, Home, Sun, Moon, Hammer } from 'lucide-angular';
import { environment } from 'environments/environment';
import { BASE_PATH } from 'app/core/modules/openapi';
import { routes } from 'app/app.routes';
import { AnalyticsService } from './analytics.service';

function initializeAnalytics(analyticsService: AnalyticsService): () => void {
return () => {
analyticsService.initialize();
};
}

export const appConfig: ApplicationConfig = {
providers: [
Expand All @@ -16,6 +23,7 @@ export const appConfig: ApplicationConfig = {
provideHttpClient(withInterceptorsFromDi()),
provideAnimationsAsync(),
importProvidersFrom(LucideAngularModule.pick({ Home, Sun, Moon, Hammer })),
{ provide: BASE_PATH, useValue: environment.serverUrl }
{ provide: BASE_PATH, useValue: environment.serverUrl },
{ provide: APP_INITIALIZER, useFactory: initializeAnalytics, multi: true, deps: [AnalyticsService] }
]
};
9 changes: 9 additions & 0 deletions webapp/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const environment = {
serverUrl: 'http://localhost:8080',
umami: {
enabled: false,
scriptUrl: '',
websiteId: '',
domains: ''
}
};
8 changes: 7 additions & 1 deletion webapp/src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const environment = {
serverUrl: 'http://localhost:8080'
serverUrl: 'http://localhost:8080',
umami: {
enabled: false,
scriptUrl: '',
websiteId: '',
domains: ''
}
};

0 comments on commit e3fd4a0

Please sign in to comment.