【发布时间】:2018-02-12 15:09:52
【问题描述】:
我有这个代码, 而且我不断收到该主题的错误。
这段代码不是我自己写的,是从网站上复制过来的,假设是从流量来源创建参数。
<script type="text/javascript">
if (localStorage.getItem('firstSource') == null)
{
var firstSource;
if ({{Page URL}}.indexOf("gclid") > -1)
{
firstSource = "AdWords"
console.log('adwords');
}
else
if ({{utm_source param}} != undefined)
{
firstSource = "{{utm_source param}} / {{utm_medium param}}"
console.log({{utm_source param}} + "utm");
}
else
if ( ({{Referrer}}.indexOf('google') > -1) || ({{Referrer}}.indexOf('bing') > -1))
{
firstSource = "{{Referrer}}"
console.log('organic');
}
else
if ( {{Referrer}} !== ")
{
firstSource = {{Referrer}}
console.log('referrer');
}
else
if ({{Referrer}} == ")
{
firstSource = "direct";
console.log('direct');
}
localStorage.setItem('source',firstSource);
dataLayer.push({'event':'setTheFirstSource','firstSource':firstSource});
}
</script>
请指教
【问题讨论】:
-
用什么替换模板字段?
-
也许你应该在询问 stackoverflow 之前尝试自己调试(见规则)
-
那段代码不能按原样工作;这最初是在将
{{ ... }}parts 替换为值的服务器或前端上运行的。 -
else 块是问题所在。如果多于一行,则 else 块应位于花括号内。
-
{{Page URL}}不是 Javascript。它是一种模板语法,应该在服务器上被实际的"http://www.someurl.com"替换,然后再发送到正确运行的浏览器。
标签: javascript token