Created
January 3, 2024 15:09
-
-
Save sterlingsky/e8dbb8f7d4770f34be9f448a9d25b991 to your computer and use it in GitHub Desktop.
Code for sending Kinect events to GA4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script> | |
| // Replace 'https://trusted-origin.com' with the | |
| // actual origin you expect to receive messages from. | |
| // no trailing slash is needed | |
| var trustedOrigin = ['https://trusted-origin.com','https://storage.googleapis.com']; | |
| window.addEventListener('message', function(event) { | |
| console.log(event) | |
| if (trustedOrigin.indexOf(event.origin) > -1) { | |
| var messageType = event.data && event.data.type; | |
| var validMessageTypes = [ | |
| 'EVENT_SEND_MESSAGE', | |
| 'EVENT_WIDGET_OPENED', | |
| 'EVENT_WIDGET_CLOSED', | |
| 'EVENT_LOCATION_SELECTED', | |
| 'EVENT_DEPARTMENT_SELECTED', | |
| 'EVENT_PROMPT_CLOSED', | |
| 'EVENT_WIDGET_ERROR' | |
| ]; | |
| if (validMessageTypes.indexOf(messageType) > -1) { | |
| window.dataLayer = window.dataLayer || []; | |
| window.dataLayer.push({ | |
| 'event': 'GA4_event', | |
| 'GA4_eventName': messageType, | |
| 'event_properties': event.data.properties | |
| }); | |
| } | |
| } | |
| else{ | |
| return; | |
| } | |
| }); | |
| </script> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to utilize GTM to set this up.
Specifically you need to build a Tag and a Trigger.
trigger setup
tag setup