This guide walks you through the process of capturing the "SwitchLive" event data emitted by the store and product locator widgets on your website using Google Tag Manager (GTM). The captured event can then be logged in your analytics or tracking system.
Step 1: Create an Event Listener for SwitchLive
Use the following JavaScript code to listen for the SwitchLive event:
window.addEventListener("message", function (e) {
if (typeof e.data.message_type != 'undefined' && e.data.message_type === 'switchlive') {
var switchLiveEvent = e.data;
console.log(switchLiveEvent, '<<< SWITCH LIVE EVENT');
// Push the event to the GTM Data Layer
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'SwitchLiveEvent',
switchLiveData: switchLiveEvent // Capture the event details
});
}
});
- Open your GTM container.
- Go to Tags > New > Tag Configuration > Custom HTML.
- Copy and paste this code snippet into the tag (wrap the code in opening and closing
<script>
tags in GTM). - Set the trigger to All Pages (or specific pages where the embed code runs).
- Save the tag.
Step 2: Create a Custom Event Trigger
- In GTM, navigate to Triggers > New > Trigger Configuration > Custom Event.
- Set the Event Name to SwitchLiveEvent (matching the event in the Data Layer).
- Name the trigger, e.g., SwitchLive Event Trigger.
- Save the trigger.
Step 3: Create a Data Layer Variable
- Go to Variables > New > Variable Configuration > Data Layer Variable.
- Set the Variable Name to switchLiveData.
- Name the variable, e.g., SwitchLive Data.
- Save the variable.
Step 4: Set Up a Tag to Capture Events
Send Data to Google Analytics 4 (GA4)
- Create a GA4 Event Tag:
- Go to Tags > New > Tag Configuration > Google Analytics: GA4 Event.
- Select your Measurement ID.
- Set the Event Name to switch_live_event.
- Add Event Parameters:
- Under Event Parameters, click Add Row.
- Set the Parameter Name to switch_live_data.
- For the Value, select the SwitchLive Data variable created earlier.
- Assign the Trigger:
- Use the SwitchLive Event Trigger.
- Save the tag and publish.
Step 5: Test the Implementation
- Open GTM in Preview Mode.
- Trigger the SwitchLive event on your website (e.g., interact with the widget).
- Verify in the GTM Preview Console:
- Check that the SwitchLiveEvent appears in the Data Layer.
- Confirm the appropriate tag fires.
- Check your analytics or logging system to ensure data is recorded.
Troubleshooting Tips
- Double-check variable and event names for typos.
- Check your browser console to ensure that the SwitchLive event is being emitted by the embed codes. You can paste the script below into your console and then begin clicking around within the widgets to ensure that the SwitchLive event is firing.
window.addEventListener("message", function(e){
if (typeof e.data.message_type != 'undefined' && e.data.message_type == 'switchlive'){
var switchLiveEvent = e.data;
console.log(switchLiveEvent, '\<\<\< SWITCH LIVE EVENT')
}
});
Please reach out to your Implementation Manager or CSM to further assist you if you have any issues.