- TLC Home Home
- Discussions Discussions
- Documentation Documentation
- Knowledge Base Knowledge Base
- Education Education
- Blog Blog
- Support Desk Support Desk
Hi guys,
I want to create a variable to get the click text.
What would be the best option for me?
thx
Hello There
This steps might solve your issue:
- In Tealium iQ, go to the Extensions tab and click "+ Add Extension".
- Select "JavaScript Code" from the list of available extensions.
- Set the scope of the extension to "DOM Ready" to ensure the extension runs after the page has loaded.
- Add the following JavaScript code in the editor:
document.addEventListener('click', function (e) {
// Get the clicked element
var target = e.target || e.srcElement;
// Get the text content of the clicked element
var clickText = target.textContent || target.innerText;
// Set the click text to a variable in the data layer (e.g., "clicked_text")
utag_data.clicked_text = clickText;
// Trigger an event for the data layer update (optional)
utag.link({ event_name: 'click_text_captured', clicked_text: clickText });
});
Save and publish your changes.
This code will listen for click events on the page, capture the clicked element's text content, and store it in a variable called "clicked_text" in the data layer. The optional utag.link() call can be used to trigger an event when the click text is captured, which you can use to trigger specific tags or actions based on the captured text.
Remember to test this on a development or staging environment before deploying to production.
Copyright All Rights Reserved © 2008-2023