How to send events with Google Tag Manager
Note: We do not offer Support in troubleshooting issues with Events in GTM.
This article is available to help guide you on what you might need to do in order to send events to Contentsquare via GTM. It is up to you and your development team to implement this functionality as it best meets your requirements.
How to create a custom HTML tag to send an event to Contentsquare
Google Tag Manager should be installed on your site before you follow this guide. If you don't have Google Tag Manager installed yet you'll need to get that set up first. Use Google's Setup and install Tag Manager article to learn how.
1. Create a new Custom HTML tag in GTM.
From the Tags section, click ‘New’
2. Click inside the Tag Configuration, then choose Custom HTML.
3. Include the script that will send the event to Contentsquare in the HTML field.
In this example, we're sending an event to Contentsquare called ‘click’. Below is the script that we will include in the HTML section of the Custom HTML tag. This means that whenever the GTM tag fires, an event will be sent to Contentsquare via our Events API called 'click'.
HTML
<script>
hj('event', 'click');
</script>
Note: The Contentsquare Tag will need to be loaded on your page before your event code
When using a GTM Custom HTML tag, the code window.hj=window.hj||function(){(hj.q=hj.q||[]).push(arguments);}; must be added inside the same <script> tags as your Event code, right above the Event. For example:
HTML
<script>
window.hj=window.hj||function(){(hj.q=hj.q||[]).push(arguments);};
hj('event', 'click');
</script>
If you don't wrap the code inside <script> tags, it may show on your site as text since GTM will inject it as regular HTML.
3. Click on Triggering and apply the relevant trigger to fire the tag.
Within the Triggering section you can also click on the + icon to add a new trigger.
In this example, we will trigger the tag to fire when a user clicks on 'All Elements'.
This means that the Custom HTML tag will fire when a user clicks anywhere on the page. When the tag fires, the ‘click’ event will be sent to Contentsquare.
4. Click on ‘Save’ to confirm your Custom HTML tag setup.
5. Click the ‘Submit’ button.
6. Ensure that the changes are being applied to the Live environment and click ‘Publish’.