Skip to main content

How to track events in Google Analytics

Send custom events to Google Analytics when the store locator loads, when users search, or when they click a location.

Updated yesterday

You can fire custom events to Google Analytics (or any analytics tool) when visitors interact with your store locator.

Tracking when the store locator loads

Add this code to your page, after the StoreRocket embed code:

<script>
document.addEventListener('storerocket-storelocator-load', function(e) {
    // Google Analytics 4
    gtag('event', 'store_locator_loaded');
});
</script>

This fires once when the store locator finishes loading on the page.

Tracking when a user searches

<script>
document.addEventListener('storerocket-storelocator-search', function(e) {
    gtag('event', 'store_locator_search', {
        search_term: e.detail.query
    });
});
</script>

This fires every time a visitor searches for an address or location. The search query is available in e.detail.query.

Tracking when a user clicks a location

<script>
document.addEventListener('storerocket-storelocator-click', function(e) {
    gtag('event', 'store_locator_click', {
        location_name: e.detail.name
    });
});
</script>

How to add the code

When installing the store locator, use Option 3 on the Widget > Install page. This gives you the embed code format that supports custom JavaScript.

Place your event tracking code right after the StoreRocket embed code on the same page.

Works with any analytics tool

These examples use Google Analytics 4 (gtag), but you can replace the gtag calls with any analytics tool. The event listeners work the same way regardless of what you send the data to.

Questions? Reach out on live chat.

Did this answer your question?