Connect GA4 directly
Audience: Integrator.
Use this procedure to send storefront events to GA4 without GTM.
Prerequisites
- Complete browser integration setup and consent configuration.
- Use a published test storefront and a test GA4 measurement ID.
- Use one Google installation per storefront document. Disable other integrations for the same GA4 property.
- Use Google Tag Manager instead if you want GTM to manage tags.
Configure the CMS record
Use this script as an alternative to GTM. Create a browser integration. Select it in the website domain record.
Select ANALYTICS_STORAGE as required consent. Set measurementId at the start of the source below.
Paste the source into JavaScript.
The script maps page, product, cart, and lead events. It sets explicit page views and disables its measurement ID during cleanup. It denies purposes that are
absent from the active consent selection. Other event names remain custom events.
Disable page-view collection in the GA4 web stream’s enhanced measurement settings, including history changes. Use one integration per destination. Reload after changing the measurement ID. Test with GA4 DebugView before release. See Google’s ecommerce event contract .
CMS script
const measurementId = 'G-EXAMPLE';
if (customerPrivacy.getState().consent.ANALYTICS_STORAGE !== true) {
throw new Error('Analytics consent is required.');
}
function googleConsent(consent) {
const purposes = {
analytics_storage: 'ANALYTICS_STORAGE',
ad_storage: 'AD_STORAGE',
ad_user_data: 'AD_USER_DATA',
ad_personalization: 'AD_PERSONALIZATION',
functionality_storage: 'FUNCTIONALITY_STORAGE',
personalization_storage: 'PERSONALIZATION_STORAGE',
security_storage: 'SECURITY_STORAGE',
};
return Object.fromEntries(
Object.entries(purposes).map(([purpose, key]) => [
purpose,
consent[key] === true ? 'granted' : 'denied',
])
);
}
function productItem(variant) {
return {
item_id: variant.sku || variant.id,
item_name: variant.product.title,
item_variant: variant.title ?? '',
price: Number(variant.price.amount),
};
}
/** Converts the local event contract to Google's documented ecommerce shape. */
function toGoogleAnalyticsEvent(event) {
const parameters = {
event_id: event.id,
page_location: event.context.document.location.href,
page_title: event.context.document.title,
page_referrer: event.context.document.referrer,
};
const data = event.data;
if (event.name === 'page_viewed') return { name: 'page_view', parameters };
if (event.name === 'product_viewed' && 'productVariant' in data) {
const variant = data.productVariant;
return {
name: 'view_item',
parameters: {
...parameters,
currency: variant.price.currencyCode,
value: Number(variant.price.amount),
items: [productItem(variant)],
},
};
}
if ('cartLine' in data) {
const line = data.cartLine;
let name = 'add_to_cart';
if (event.name === 'product_removed_from_cart') name = 'remove_from_cart';
return {
name,
parameters: {
...parameters,
currency: line.merchandise.price.currencyCode,
value: Number(line.merchandise.price.amount) * line.quantity,
items: [{ ...productItem(line.merchandise), quantity: line.quantity }],
},
};
}
if (event.name === 'cart_viewed' && 'lines' in data) {
return {
name: 'view_cart',
parameters: {
...parameters,
currency: data.total?.currencyCode,
value: Number(data.total?.amount ?? 0),
items: data.lines.map((line) => ({
...productItem(line.merchandise),
quantity: line.quantity,
})),
},
};
}
if (event.name === 'form_submitted' && 'formId' in data) {
return {
name: 'generate_lead',
parameters: { ...parameters, form_id: data.formId },
};
}
return { name: event.name, parameters };
}
window.dataLayer ??= [];
function gtag() {
window.dataLayer.push(arguments);
}
const scriptId = 'hito-ga4-' + measurementId;
const disabledKey = 'ga-disable-' + measurementId;
window[disabledKey] = false;
if (!document.getElementById(scriptId)) {
gtag('consent', 'default', googleConsent(customerPrivacy.getState().consent));
gtag('js', new Date());
const script = document.createElement('script');
script.id = scriptId;
script.async = true;
script.src =
'https://www.googletagmanager.com/gtag/js?id=' +
encodeURIComponent(measurementId);
document.head.appendChild(script);
} else {
gtag('consent', 'update', googleConsent(customerPrivacy.getState().consent));
}
gtag('config', measurementId, { send_page_view: false });
customerPrivacy.subscribe((state) => {
gtag('consent', 'update', googleConsent(state.consent));
});
analytics.subscribe('all_events', (event) => {
const mapped = toGoogleAnalyticsEvent(event);
gtag('event', mapped.name, { ...mapped.parameters, send_to: measurementId });
});
runtime.onDispose(() => {
window[disabledKey] = true;
gtag('consent', 'update', googleConsent({}));
});Verify and publish
- Start with analytics consent denied. Make sure the CMS script does not load GA4.
- Grant analytics consent. Make sure the script loads once and advertising consent remains separate.
- View a product, add two units, remove one unit, and open the cart.
- Examine mapped event names, numeric values, and item arrays in GA4 DebugView.
- Repeat with a product without a SKU. Make sure the event uses its variant ID.
- Navigate between pages without a full reload. Confirm one
page_viewper application page view. - Make sure failed cart actions create no action event. A cart refresh must not send another
add_to_cart. - Withdraw analytics consent. Make sure forwarding stops and Google consent changes.
- Examine vendor listeners. Removing a loader element does not unload the SDK. Reload when full removal is necessary.
- Select the integration on the production Website-Domains record. Set the production measurement ID, then enable and save it.
- Confirm that only one integration sends to the production property. Record the saved CMS configuration with the release.
Use the mapping reference for quantities, value units, and unsupported events. Test with GA4 DebugView before release. Reload after you change the measurement ID.