如果wordpress postid是偶数且GTM

时间:2017-07-31 16:56:18

标签: javascript wordpress google-tag-manager

我尝试使用以下逻辑通过GTM添加InRead广告:



<script>
  
function set_ad_player(){
var el = document.querySelector('section[data-postid]');
 var idp = el.getAttribute('data-postid');

    
   if (idp%2 == 0) {
    window._ttf = window._ttf || [];
    _ttf.push({
        pid          : 0202020
        ,lang        : "es"
        ,slot        : '.entry-content > p'
        ,minSlot     : 4,
        format       : "inread"
        ,components  : { mute: {delay: 6}, skip: {delay :6} }
        ,css         : "margin: 27px 0px 28px;padding: 0px 9px;"
    });

    (function (d) {
        var js, s = d.getElementsByTagName('script')[0];
        js = d.createElement('script');
        js.async = true;
        js.src = '//cdn.teads.tv/media/format.js';
        s.parentNode.insertBefore(js, s);
    })(window.document);

 console.log(idp);
   } else{
    (function() {
      window["SMIntextSetUp"] = {
        c:'.entry-content > p',
        adPosition:4,
        viewable_desktop:true,
        viewable_mobile:true,
        adSkip:true,
    restart:true,
        tag_desktop:'https%3A%2F%2Fvod.addevweb.com%2Fsunmedia%2Fdemos%2Ftags%2Fnormal.xml',
        tag_mobile:'https%3A%2F%2Fvod.addevweb.com%2Fsunmedia%2Fdemos%2Ftags%2Fnormal.xml'
      };
      var a,t,s,n;
      t = document.createElement("script");
      t.async = true;
      t.onerror=function(){if(SMIntextSetUp.passback!=null)window[SMIntextSetUp.passback]();};
      t.type = "text/javascript";
      s="https:" == document.location.protocol;
      t.src = (s ? "https:" : "http:") + "//static.addevweb.com/SMOutstream/SMIntext/SMIntext.js";
      n = document.getElementsByTagName("script")[0];
      n.parentNode.insertBefore(t, n);
    })();
  }
  
  }


  set_ad_player()


</script>
&#13;
&#13;
&#13;

我的逻辑基于通过Javascript获取Wordpress postID,然后将属性传递给条件,该条件将确定postid数字是偶数还是奇数。

Google跟踪代码管理器会在每一个Wordpress中启动它。

如果条件正确并且必须在DomReady或WindowLoad中触发此代码,我的问题是相关的。

你有什么建议?

1 个答案:

答案 0 :(得分:1)

您的情况不正确。你应该将字符串转换为整数

.....
var idp = parseInt(el.getAttribute('data-postid'));    
if (idp%2 == 0) {
.....

你应该在domready之后调用你的代码。应该是这样的:

<script>
(function() {
    //use your code here
})();
</script>