D.Feltimo
D.Feltimo

Reputation: 13

Google Tag Manager - Returning a href from another element when a click tag is fired

I'm working on Google Tag Manager/Analytics for a site, here's an example page that a tag is being fired on:

https://www.forktrucktraders.co.uk/listings/refurbished-combilift-multi-directional-gas/

The tag is fired when the "Send Message" button on the contact form is clicked:

https://i.sstatic.net/SRxT0.jpg

Right now I've got the event's action returning the URL of the current page the form was sent from, but I'd like to know if it's possible to get the href from the "Visit dealer's website" link on the page, as it would give a faster idea of which dealer the listing is coming from. Probably a long shot to make this happen solely through Tag Manager (if not possibly just a hidden bit of data that just has the dealer's name in on the "Send Message" button) but I'd appreciate any input.

Upvotes: 0

Views: 1380

Answers (2)

Josh berry-jenkins
Josh berry-jenkins

Reputation: 11

You most certainly can. Off the top of my head something along the lines of the following should work...

It depends on whether you prefer just having the url or breaking it down further.

Just the URL:

Create the following in a custom HTML tag within GTM

<script>
//This selects your desired href:

var dealerURL = document.querySelector(".stm-dealer-image-custom-view a").href;

//This pushes the value into the dataLayer temporarily for use later:

window.dataLayer.push({
event: 'formSubmit',
dealer: dealerURL
})
</script>

Ideally, this should be fired on page load of all your listings pages.

  • Next create a new User-Defined Variable of the dataLayer var type within GTM corresponding to dealer, this will store the value to be pulled through in your event.
  • Now just change your event action to {{dealer}} (or whatever you ended up naming the datalayer variable), and this value should be pulled through in your event.

Getting the dealer name:

Now presuming the href format is always the same across the site you could split this by a delimiter instead:

var dealerURL = document.querySelector(".stm-dealer-image-custom-view a").href;
var dealerSplit = dealerURL.split("/");
var dealer = dealerSplit[4];

The above would leave you with a variable containing the string 'hitec'.

This however is quite prone to breaking if the page path does not always follow the same format, as such it would be better to use RegEx instead.

Upvotes: 1

XTOTHEL
XTOTHEL

Reputation: 5208

To answer your specific question, you would need to create a variable to target that specific link element that contains the dealer's website's url. The simplest way is probably to add an id to the <a> element and create a variable in GTM to track it.

I had a quick look at your site and I think you have more problems with the form.

  1. Your even triggers without validating the form, this would lead to extra events.
  2. The event category, action and label could use some work in organizing it to help you analyze the data
  3. You also have a mix of gtag.js and GTM snippet on the page, I would say this is not normal practice, usually, GTM is enough. You can contact me through my profile if you'd like to chat more about it.

Upvotes: 0

Related Questions