Tracking HTML links with Zaraz
How can i track clicks on ahref links for GA4 with Zaraz? I tried adding Click listner and XPath with the /link to track and also the event tag for GA4 but it doesnt seem to work
8 Replies
I'm using this feature and it's working for me. But XPath can be complicated... do you have the option to modify the HTML of the site and add a CSS class or Id to the target element(s)? Might be more reliable/simple. For example I add a
cta-try-app
class on my site for all call to action links that I wanted Zaraz to track for me, then added a unique element id for each CTA and forward both the click and the link id to google analytics.My click trigger looks like this:

Then my GA4 action looks like this:

The element_id lets me know which instance of the CTA links a user clicked.
I see. I don't have a css class on my ahref links so i just need to track the url itself
You can do it with other selectors - but its just going to be a matter of really verifying that the XPath thing works the way you think it does! (ie: not a bug in Zaraz, just the intricacy of DOM traversal).
Use css selector a[href="your link"]
or a[href^="start with"] using ˆ
or end with a[href$="ends with"]
or includes a[href*="has it"]
Like this for all URLs containing /go/ ?
