Track map events in Google Analytics (5 minutes)
Mapplic fires an event when a visitor opens a location or enables a group filter. Forward these events to Google Analytics to see which locations and groups people use. This tutorial connects an embedded map to a GA4 property.
You need an embedded map and a GA4
property with its gtag.js snippet already on the page. The
Events section of the JavaScript API reference
describes the underlying API.
Embed the map with an id
Embed the map and give the element an id so JavaScript can target it — here my-map:
<mapplic-map
id="my-map"
data-json="https://mapplic.com/getMapData?id=6AAA3HVY6u05TYjJvG42"
><script type="text/javascript" id="mapplic-script" src="https://mapplic.com/mapplic.js"></script></mapplic-map>Wait for the map to be ready
The store that exposes the event API becomes available once the mapReady event fires
on the element. Attach a listener and read the store inside it:
const map = document.getElementById('my-map');
map.addEventListener('mapReady', () => {
const store = map.store;
// event registration goes here
});Forward map events to Google Analytics
Register an event listener on the store and pass each event to gtag. Prefixing the
names with mapplic_ keeps the map’s events easy to find in your GA data:
map.addEventListener('mapReady', () => {
map.store.getState().registerEventListener((name, data) => {
gtag('event', `mapplic_${name}`, data);
});
});Two events reach GA4:
| GA4 event | Parameters | Fired when |
|---|---|---|
mapplic_location | id, title, layer | A location is opened. |
mapplic_group | name | A group filter is enabled. |
Verify the events
Interact with the map — open a few locations, toggle a group filter — and check that
the mapplic_location and mapplic_group events arrive in your GA4 property’s
realtime or debug view.
Event parameters (id, title, layer, name) may need to be registered as custom
dimensions in GA4 before they show up in standard reports. Until then GA4 records only
the event counts. See the GA4 documentation for the current steps.
The same listener works with any analytics tool. Replace the gtag call with Matomo,
Plausible, a Google Tag Manager dataLayer.push, or a custom endpoint. The full event
API is documented in the JavaScript API reference.