Discover step‑by‑step instructions to integrate Lovable with Google Analytics. Learn how to track your metrics efficiently and maximize insights.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Create a new TypeScript file named AnalyticsConstants.ts in your project's source folder (for example, in the src directory). This file will hold your Google Analytics Tracking ID. Open the file and paste the following code:
export const GATRACKINGID = 'G-XXXXXXXXXX'; // Replace with your actual Google Analytics Tracking ID
Next, create another TypeScript file named AnalyticsLoader.ts in the same directory (src). This file will be responsible for dynamically loading the Google Analytics script. Since Lovable does not have a terminal to install dependencies, we will use vanilla TypeScript to append a script tag to your index HTML file. Paste the following code into AnalyticsLoader.ts:
import { GATRACKINGID } from './AnalyticsConstants';
export function loadGoogleAnalytics(): void {
// Do not load if already loaded
if (document.getElementById('ga-script')) return;
const script = document.createElement('script');
script.id = 'ga-script';
script.async = true;
script.src = https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID};
document.head.appendChild(script);
const inlineScript = document.createElement('script');
inlineScript.innerHTML = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GATRACKINGID}');
`;
document.head.appendChild(inlineScript);
}
Find the main file where your Lovable project’s application initializes. This might be a file like App.ts, main.ts, or another entry point. Insert the following import and function call at the very beginning of your initialization code to ensure Google Analytics loads when the application starts.
import { loadGoogleAnalytics } from './AnalyticsLoader';
// Initialize Google Analytics as soon as the app starts
loadGoogleAnalytics();
// Continue with the rest of your app initialization code...
After you save all changes and reload your Lovable project in the browser, open your browser’s developer tools and check the Network tab for the request to gtag/js. Additionally, you can verify on your Google Analytics dashboard that the data is being received.
If you need to track events beyond page views, you can create additional functions in your AnalyticsLoader.ts file. For example, add the following function to log custom events:
export function trackEvent(category: string, action: string, label?: string, value?: number): void {
if (typeof window.gtag !== 'undefined') {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value
});
}
}
You can then call this function in your other parts of the project wherever custom event tracking is needed.
By following these steps and placing the code snippets in the respective files, you successfully integrate Google Analytics into your Lovable project without needing a terminal for dependency installation.
This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.