如果语句为真,如何调用javascript跟踪代码?

时间:2016-10-20 08:22:55

标签: javascript google-adwords tracker

我必须向网站插入2个跟踪代码,但我还必须确保它们仅在jQuery语句为真时跟踪活动。一个是Google Adwords,另一个是Gemius代码:

以下是Gemius跟踪代码:

<script type="text/javascript">
    (_gdeaq = window._gdeaq || []).push(['pageView', 'gdehu', 'some_code', 'some_code_2, 'some_code_3']);
</script>
<script type="text/javascript" src="//gdehu.hit.gemius.pl/something_url"> </script>

的Adwords:

<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 12345678;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "some_data_here";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>

<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/12345678/?label=some_data_here&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

如果声明为真,我必须激活这两个跟踪器:

<script type="text/javascript">

    jQuery(function(){
        jQuery("#formsubmit").click(function(event){
            //some code here

            jQuery("input[type='text'],select,input[type='checkbox']","#forms").each(function(){

            //some code here

            });

            if( jQuery("input[name='email']","#forms").val() != jQuery("input[name='cEmail']","#forms").val() ){
                //some code here
            }

            if(!error_flag){
                //i have to call the trackers only if this statement is true 

                //so i want to insert them here somehow
            }else{
                alert(error_message);
            }
        });
    }); 
</script>

你能告诉我一些解决方案吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

if(!error_flag){
   //i have to call the trackers only if this statement is true 
   // i want to insert them here somehow
  (function() {
  (_gdeaq = window._gdeaq || []).push(['pageView', 'gdehu', 'some_code', 'some_code_2, 'some_code_3']);
  var sc1 = document.createElement('script'); sc1.type = 'text/javascript'; sc1.id = "sc1";
  sc1.src = '//gdehu.hit.gemius.pl/something_url';
  var fs = document.getElementsByTagName('script')[0];   
  fs.parentNode.insertBefore(sc1, fs);
  })();


  var google_conversion_id = 12345678;
  var google_conversion_language = "en";
  var google_conversion_format = "3";
  var google_conversion_color = "ffffff";
  var google_conversion_label = "some_data_here";
  var google_remarketing_only = false;
  (function() {
  var sc2 = document.createElement('script'); sc2.type = 'text/javascript'; sc2.id = "sc2";
  sc2.src = '//www.googleadservices.com/pagead/conversion.js';
  var fs = document.getElementsByTagName('script')[0]; 
  fs.parentNode.insertBefore(sc2, fs);
  })();

}