Click text when using the Google Tag Manager - gtm.click {{Click Text}}

I have a list of shops with detailed information like opening hours and phone numbers. When a user clicks on the shop's name, I want the relevant details to appear using javascript.

I am trying to capture the shop's name (SHOP NAME) when a user clicks on it using Tag Manager, and then display this information in Google Analytics. However, my attempts in GTM have been unsuccessful so far.

Any assistance would be greatly appreciated.

Here is the code snippet:

<table>
  <tbody>
    <tr class="row" valign="top">
        <td class="locationGridColZip" onclick="javascript: locationGridAddressToggle(this);">
          <input type="hidden" name="ctl00$ContentRegion$ctl00$locationGridView$ctl02$hfStore" id="ctl00_ContentRegion_ctl00_locationGridView_ctl02_hfStore" value="2620|55,65739|12,35353">CITY
            </td>
            <td class="locationGridColImgOFF" onclick="javascript: locationGridAddressToggle(this);">
            </td>
            <td class="locationGridColAddress" onclick="javascript: locationGridAddressToggle(this);">
              <h3>SHOP NAME</h3>                    
            </td>
            <td class="locationGridColShow">
              <a onclick="javascript:map.setCenter(new google.maps.LatLng(55.65739, 12.35353));map.setZoom(12);" href="/butikker.aspx#gmap">SHOW ON MAP</a>
              <a class="countryGmap countryGmap1" onclick="javascript:map.setCenter(new google.maps.LatLng(55.65739, 12.35353));map.setZoom(12);" href="/butikker.aspx#gmap"><img src="https://cdn.fotoagent.dk/webshop/production_solr/images/gmapDanmarkAlt.gif" alt="Vis på kort" style="border-width:0px;"></a>
      </td>
    </tr>
  </tbody>
</table>

Answer №1

For those using jQuery, you can integrate it into a custom JavaScript variable like this (modify as necessary):

function(){
   try{
      var element = {{Click Element}};
      var locationName = $(element).closest('tr').find('.locationGridColZip').text().trim();
      if (locationName.length > 0){
         return locationName;
      }
      return 'undefined';
   }
   catch(error){
   }
}

Answer №2

If you want to implement this feature using a custom JavaScript variable, you'll need to consider the specific context of your situation. Depending on which element the 'click element' refers to when triggering the tag, you can use a code snippet like this:

function ()
{
  return {{Click Element}}.querySelectorAll("h3");
}

For more examples of custom JavaScript variables, check out this resource: http://www.example.com/custom-javascript-variable-examples/

You can also find a comprehensive guide by Simo Ahava here:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What sets defineProps<{({})}>() apart from defineProps({ }) syntax?

While examining some code written by another developer, I came across the syntax defineProps<({})>(). After doing some research, I couldn't find any resources that helped me understand this particular syntax. My Way of Defining Props defineProp ...

Is it possible to transform a webpack configuration into a Next.js configuration?

i have come across a webpack configuration setup from https://github.com/shellscape/webpack-plugin-serve/blob/master/recipes/watch-static-content.md: const sane = require('sane'); const { WebpackPluginServe: Serve } = require('webpack-plugin ...

How can we reset multiple selected values (mui chips) in a React Material-UI Autocomplete field when changing the value in a different field?

Is there a way to clear the mui-chips in Material UI Autocomplete TextField when the value in another field is changed? I have been struggling with clearing the subtype value when the Type value changes. Although I can update the drop-down options based on ...

Is there a potential security flaw in jQuery when using $.get(code_url) to execute the returned code without relying on eval() or appending it to the DOM

As I was debugging my NodeJS application, I stumbled upon a surprising discovery: when using jQuery with a simple $.get(code_url) where code_url is a URL leading to server-side JavaScript code, the code gets executed. The expected behavior should be: $.g ...

Determine browser compatibility by evaluating the DOM Level

Can we easily determine which browser versions and above are compatible with a certain DOM level? ...

What is the best way to retrieve a linked filter in Vue from another?

I have created a file to store filters linked to my Vue object, and it is being imported into my App.js. One of the filters I have needs to use another filter: Vue.filter('formatDateTime', value => { if (value) return moment(String(value ...

How can I create a fading trail effect in Three.js that diminishes over time?

I am interested in achieving a similar effect to the example I found on this website: However, my goal is to have the old trail gradually fade into the background over time instead of cluttering the screen with persistent marks. I discovered that by usin ...

What is the best way to center a fixed position background image within a container that is slightly shifted from the top of the viewport?

How can I center a background-image vertically, which has a fixed background-attachment and is positioned 100px from the top? The background-size property is set to cover for horizontal centering but the vertical alignment is off. Below is the HTML code: ...

Ways to retrieve the ID attribute value of the third ancestor above

How can I retrieve the ID attribute of the 3rd parent element when a link is clicked? Take for example: <div id="First Div"> <div class="class A"> <div class="class B"></div> <div class="class C"></div> &l ...

What is the method for incorporating a variable in MapReduce?

I have the following MapReduce script that I'm working with: splitAndGroupServices = function(groupMembers) { var mapFunction = function() { for(var idx in this.services) { var service = this.services[idx]; if(service.mem ...

Differentiating a path element in SVG by dynamically adding color using d3 and react

I have a unique SVG icon: <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> <circle cx="12" cy="12" r="12" /> <path class="svg-fill" d="M12,2A10,10,0,1,0,22, ...

Vue Component Unit Testing: Resolving "Unexpected Identifier" Error in Jest Setup

Having just started using Jest, I wanted to run a simple unit test to make sure everything was set up correctly. However, I encountered numerous errors during compilation while troubleshooting the issues. When running the test suite, Jest successfully loc ...

As we iterate through each individual array within the multi-dimensional array

I'm currently working on an application using node.js, Express, and JS/Jquery. One issue I've encountered is with appending elements to a webpage based on the number of arrays contained in a MD array. Essentially, I only want to append unique e ...

Encountering issues with reading undefined properties while working with react-chartjs-2 and chart js

Having trouble with react chartjs errors? Visit the link for more details https://i.stack.imgur.com/lI2EP.png The versions I'm using are ^3.5.0 for chart.js and ^4.0.1 for react-chartjs-2 Tried downgrading to version 2 but it didn't solve the ...

Is the text in the React chat application too lengthy causing a bug problem?

In my chat application built with React, I am facing an issue where if a user types more than 100 characters, the message gets cut off. How can I fix this problem? Please refer to the image below for reference. https://i.sstatic.net/DLNyH.png {Object.keys ...

Apollo-Server presents errors in a polished manner

It seems like the question explains itself adequately. I am currently using 'apollo-server-core' version 3.6.5 Desired Errors: { "errors": [ { "message": "Syntax Error: Unexpected < ...

I have the ability to effectively open a modal whenever necessary, but I struggle with closing it afterwards

I've been working on implementing a React bootstrap modal, and while I can successfully open it when needed, I'm running into issues with closing it. I'm not sure what mistake I might be making. Here's my markup: <Modal show={this. ...

Submitting Multi-part forms using JQuery/Ajax and Spring Rest API

Recently, I started exploring JQuery and decided to experiment with asynchronous multipart form uploading. The form includes various data fields along with a file type. On the server side (using Spring), I have set up the code as follows: @RequestMapping ...

Troubleshooting issue with file upload feature in Angular for Internet Explorer 9

I have implemented a file upload method using the following code: <input type="file" name="upload-file" ng-model= "excelFile" accept=".xlsx" onchange="angular.element(this).scope().fileChanged(this);" ...

Using a for loop, how can the property value be set in another object?

One challenge that I am facing is setting object property values. The object in question looks like this: const newPlan = { name: '', description: '', number: '', monday: { breakfast: '', ...