Search code examples
google-tag-manager

Google Tag Manager Variable based on Trigger


I've greated 8 different visibility trigger for different forms based on CSS Selectors (for example: The newsletter would be: input[value="Sign up to our Newsletter"] and the contact form would be input[value="Send"]. (Most likely there is a more elegant way to do this).

Now I want to create just one view_form GA4 Event Tag but would like to give the form name in a parameter. So, if it is triggered by the newsletter --> form_name should be "newsletter" and if is triggered by the contact form --> form name would be "contact form". Can I built a variable based on the trigger or is there a better way?

Thanks


Solution

  • First and foremost, visibility triggers are to be avoided. They're considered dirty implementation even in otherwise dom-scraping implementations.

    Now, you could try two things.

    1. Try auto event variable to get the element and then reference it in a CJS variable to see what it is and return appropriate string.

    2. Do everything in CJS, using a global document selector instead of the auto event variable to determine what's visible and what triggered your visibility triggers.

    The pProper way to track things like these would be, of course, via the dataLayer.