How to check a class for a certain value and then return a result through Tag Manager

MrSponge

1: What I want to do is check if a page contains a specific class with a specific string, and if it does, send an event to Google Tag Manager.

2: I also need a macro to check if a certain element exists on a page and if it does, fire the tag above.

What I have been able to get together so far is the supposed tag:

<html>
<head></head>
<body>
...
<a class="myClass">Captured value</a>
...
</body>
</html>

My tag to be fired using JavaScript would look something like this:

<script>
var x = document.getElementsByClassName("myClass")[0].innerHTML;
if (x.match ("Captured value")) {
dataLayer.push({'event':'captured'});
}
</script>

So this tag will fire if it sees the above anchor with that specific class. Using the above code though, I keep getting errors:

Uncaught Type Error: Cannot read property 'innerHTML' of undefined(anonymous function) 
@VM490:2InjectedScript._evaluateOn 
@VM479:883InjectedScript._evaluateAndWrap 
@VM479:816InjectedScript.evaluate 
@VM479:682

How would I write a cleaner code to not generate an error if the class doesn't exist?

Regarding number two, how would I write a function with a return statement? I just can't figure this one out. I have tried something along the lines of:

function () {
var t = document.getElementsByClassName("myClass")[0].innerHTML;
var y = dataLayer.push({'event':'trigger'})
if (t.match ("Captured value")) {
return y;
}

I want to use a macro to check if the class exists on a page with the correct value, which in turn I can use as a trigger to fire the first tag. To use macros in GTM, I need to return a value. I just can't figure out how to check for the class value and have my tag fire upon its validation.

Another way to go is of course to write a custom HTML tag to fire on all pages and have the JavaScript look for, validate and send the event. I currently do not possess the JavaScript aptitude to figure out how that would look though.

Any suggestions are highly appreciated.

MrSponge

My approach was all backwards. I attempted to use a JavaScript solution while I could just use a custom JS inside of Tag Manager to check for a defined value and fire a trigger if it resolved to true.

The methodology I was thinking about was about right I guess, but the way about doing it was over complicating things.

What I did was add a custom JavaScript macro named cjs.return class (using v1 for this said container) and added the following code:

function (){
    x = document.getElementsByClassName("myClass")[0].innerHTML;
    return x;
}

This function returned either undefined or "Captured value" depending on if it was the right page or not. I then set up a new rule to trigger my tag if:

  • event equals gtm.dom
  • cjs.return class equals Captured value

This new rule triggered my tag, which in turn could send the data on the specific set of pages I wanted it to fire on.

Thanks to both Eike and nyuen for answering to give me a clearer picture of what I did wrong.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I loop through certain records, check a query, and conditionally assign field value using VBA?

분류에서Dev

Can I return a text value if a certain tag is found?

분류에서Dev

Send new value to Google Tag Manager through dataLayer from fancybox (ajax)

분류에서Dev

How to parse through a table to gather tide value at a certain time of day

분류에서Dev

Loop through all UITextViews with certain tag number

분류에서Dev

JSP Tag file that outputs the result (return value) to a variable instread of printing it out

분류에서Dev

How do I return the result of a class-based view from another class-based view in Django?

분류에서Dev

how to check in an XMLTree if it and return

분류에서Dev

How to Check value id from Tag Input and remove count row is 0?

분류에서Dev

How to hide a class when a certain class is active?

분류에서Dev

Check shell command return value in c

분류에서Dev

How to check for a value in an array

분류에서Dev

How to check the first letter of a Button.Tag?

분류에서Dev

How to return a failure inside .map function in a Result

분류에서Dev

How to return the onclick function is result to href in javascript?

분류에서Dev

How to return JSON result by using LINQ in WCF?

분류에서Dev

Loop through an array of names and check if value of keypress matches a character in a name

분류에서Dev

SQL (MS SQL Server) return column value as the result of the multiplication

분류에서Dev

How to search through all keyboard shortcuts that use a certain key?

분류에서Dev

Raise an exception if class attribute is set to a certain value after instantiation

분류에서Dev

How to check $addToSet return true or false in mongodb?

분류에서Dev

How to manipulate an uncreated XML document through a class

분류에서Dev

check_box_tag is only passing the last check box value to params

분류에서Dev

How to choose attribute for a tag with no value in javascript

분류에서Dev

How do I return rows in groups by certain values?

분류에서Dev

How to check if passed arguments in a class are correct

분류에서Dev

How to return nested class pointer from method?

분류에서Dev

How pass javascript function value into input tag value?

분류에서Dev

How can i return a Django ModelForm through an Jquery Ajax call?

Related 관련 기사

  1. 1

    How do I loop through certain records, check a query, and conditionally assign field value using VBA?

  2. 2

    Can I return a text value if a certain tag is found?

  3. 3

    Send new value to Google Tag Manager through dataLayer from fancybox (ajax)

  4. 4

    How to parse through a table to gather tide value at a certain time of day

  5. 5

    Loop through all UITextViews with certain tag number

  6. 6

    JSP Tag file that outputs the result (return value) to a variable instread of printing it out

  7. 7

    How do I return the result of a class-based view from another class-based view in Django?

  8. 8

    how to check in an XMLTree if it and return

  9. 9

    How to Check value id from Tag Input and remove count row is 0?

  10. 10

    How to hide a class when a certain class is active?

  11. 11

    Check shell command return value in c

  12. 12

    How to check for a value in an array

  13. 13

    How to check the first letter of a Button.Tag?

  14. 14

    How to return a failure inside .map function in a Result

  15. 15

    How to return the onclick function is result to href in javascript?

  16. 16

    How to return JSON result by using LINQ in WCF?

  17. 17

    Loop through an array of names and check if value of keypress matches a character in a name

  18. 18

    SQL (MS SQL Server) return column value as the result of the multiplication

  19. 19

    How to search through all keyboard shortcuts that use a certain key?

  20. 20

    Raise an exception if class attribute is set to a certain value after instantiation

  21. 21

    How to check $addToSet return true or false in mongodb?

  22. 22

    How to manipulate an uncreated XML document through a class

  23. 23

    check_box_tag is only passing the last check box value to params

  24. 24

    How to choose attribute for a tag with no value in javascript

  25. 25

    How do I return rows in groups by certain values?

  26. 26

    How to check if passed arguments in a class are correct

  27. 27

    How to return nested class pointer from method?

  28. 28

    How pass javascript function value into input tag value?

  29. 29

    How can i return a Django ModelForm through an Jquery Ajax call?

뜨겁다태그

보관