Easily integrate Lovable with Segment using our step-by-step guide. Streamline your data tracking and boost your marketing insights today.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
analytics.ts inside the project’s main code folder (for example, the src folder).
declare global {
interface Window {
analytics: any;
}
}
export const initSegment = (writeKey: string) => {
// Check if Segment was already initialized to avoid duplicate loading
if (window.analytics && window.analytics.initialized) {
return;
}
(function() {
const analytics = (window.analytics = window.analytics || []);
if (!analytics.initialize)
if (analytics.invoked) {
console.error("Segment snippet included twice.");
return;
} else {
analytics.invoked = true;
analytics.methods = [
"trackSubmit", "trackClick", "trackLink", "trackForm", "pageview",
"identify", "reset", "group", "track", "ready", "alias", "debug", "page", "once", "off", "on"
];
analytics.factory = function(method: string) {
return function() {
const args = Array.prototype.slice.call(arguments);
args.unshift(method);
analytics.push(args);
return analytics;
};
};
for (let i = 0; i < analytics.methods.length; i++) {
const key = analytics.methods[i];
analytics[key] = analytics.factory(key);
}
analytics.load = function(key: string) {
const script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = "https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";
const first = document.getElementsByTagName("script")[0];
if (first && first.parentNode) {
first.parentNode.insertBefore(script, first);
}
};
analytics.SNIPPET_VERSION = "4.1.0";
}
// Load the Segment analytics library using your provided key
analytics.load(writeKey);
analytics.page();
analytics.initialized = true;
})();
};
export const trackEvent = (eventName: string, properties?: object) => {
if (window.analytics && window.analytics.track) {
window.analytics.track(eventName, properties);
}
};
index.ts or similar file.initSegment function from the analytics.ts file you just created.
import { initSegment, trackEvent } from "./analytics";
// Initialize Segment analytics using your write key here.
// Replace "YOURWRITEKEY" with your actual Segment write key.
initSegment("YOURWRITEKEY");
// Other app initialization code can go here
// Example: Tracking a page view or any event later in your code.
trackEvent("Page Viewed", { page: "Home" });
trackEvent function from analytics.ts to send event data to Segment.
import { trackEvent } from "./analytics";
// Tracking a custom event when a specific action occurs, for example:
const onUserAction = () => {
trackEvent("User Action", { action: "Clicked Button", label: "Subscribe" });
};
// Ensure to call onUserAction when appropriate in your code.
initSegment function.
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.