onclick return two functions (Tracking)


I'm trying to trigger two functions on the onclick event. They are used for tracking link from website. One is for GoogletagManager, the other one is for AdForm tracking.

I get error that the function is not defined. Any solutions to make this work?

<a href="https://www.example.com" target="_blank" onclick="myFunction();">Link</a>

  <script type="text/javascript">

  function myFunction(){
      secondone();
      firstone();
  }
  function firstone() {
      return console.log('first');
      return gtag_report_conversion1('http://www.example.com');
  }
  function secondone() {
      return console.log('second');
      return window.adf&&adf.ClickTrack ( this,{123456},'Page Name',{});
  }

  </script>
  <script>
function gtag_report_conversion1(url) {
  var callback = function () {
    if (typeof(url) != 'undefined') {
      window.location = url;
    }
  };
  gtag('event', 'conversion', {
      'send_to': 'AW-465465465465465',
      'event_callback': callback
  });
  return false;
}
</script>
<script type="text/javascript">
      window._adftrack = Array.isArray(window._adftrack) ? window._adftrack : (window._adftrack ? [window._adftrack] : []);
      window._adftrack.push({
          pm: 123456,
          divider: encodeURIComponent('|'),
          pagename: encodeURIComponent('Page Name')
      });
      (function () { var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = 'https://track.adform.net/serving/scripts/trackpoint/async/'; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); })();

  </script>
- - Source

Answers

answered 1 week ago Nathan Blaine #1

After reading your edit - try moving all of the functions to the same <script></script> tag :)

answered 1 week ago Joseph Cho #2

EDIT: It looks like the line return window.adf&&adf.ClickTrack ( this,{123456},'Page Name',{}); is breaking secondone(). This breaks myFunction() in turn when it's called.

For example, the following script works:

<a href="https://www.example.com" target="_blank" onclick="myFunction()">Link</a>

<script>
  function myFunction() {
      firstone();
  }

  function firstone() {
      return console.log('first');
      return gtag_report_conversion1('http://www.example.com');
  }
</script>

I'm not sure where you got the javascript from, but if you fix the error line myFunction() will be defined.

comments powered by Disqus