根据推荐人、用户代理显示不同的电话号码

发布于 2024-10-14 15:37:45 字数 1596 浏览 4 评论 0原文

我尝试根据多种因素在网站上提供不同的电话号码,从而使我们能够根据用户到达页面的方式来跟踪入站电话。

  1. 通过检查“?provider=”查询字符串来检查用户是否来自广告,如果存在,则提供广告号码
  2. 如果不存在,请检查 userAgent 以向机器人提供“默认”号码
  3. 如果 userAgent 不是机器人,请检查引荐来源网址并提供不同的跟踪基于常见 SERP 点击次数的数字

该网站无法运行服务器端脚本,因此必须使用 javascript 来完成(我对此相当缺乏经验)。我已经将一些零碎的东西拼凑在一起,但最终希望将所有内容组合起来,并使其在一个可以在页面加载时调用的单个 .js 文件中一起工作。

到目前为止,我得到的信息如下:

<script type="text/javascript">
//Querystring to check if paid session or organic
     function providercheck() {
        var str= location.href;
            if (str.indexOf("?provider=") > -1) {
                document.write("paid phone number");
            }
            else {
                botcheck();
            }
    }

    //Check userAgent to serve organic number to bots
     function botcheck() { 
            if(navigator.userAgent.toLowerCase().indexOf("googlebot") <= -1) {
                    document.write("default phone number");
                }
            else {
                    refcheck();
                }
    }

    //Check for referrer and serve number based on last referrer
    function refcheck() {
        var last_referrer = document.referrer;
        if (last_referrer.indexOf("google") > -1) {
            document.write("google tracking number");
        }
        else if (last_referrer.indexOf("yahoo") > -1) {
            document.write("yahoo tracking number");
        }
        else {
            document.write("default number");
        }
    }
</script>

非常感谢任何帮助或指导。我还需要设置一个会话 cookie,以继续提供相同的号码,直到用户离开网站,但我想先让它工作

I am attempting to serve different phone numbers on a site based on a number of factors, allowing us to track inbound phone calls based on how users arrived at the page.

  1. Check to see if user is coming from advertising by checking for "?provider=" querystring, serve advertising number if present
  2. If not, check userAgent to serve "default" number to bots
  3. If userAgent is not a bot, check referrer and serve different tracking number based on common SERP clicks

This site is unable to run server-side scripting so this has to be done with javascript (which I am fairly inexperienced with). I've cobbled together bits and pieces but ultimately would like to combine everything and have it work together in a single .js file that can be called on page load.

Here's what I have so far:

<script type="text/javascript">
//Querystring to check if paid session or organic
     function providercheck() {
        var str= location.href;
            if (str.indexOf("?provider=") > -1) {
                document.write("paid phone number");
            }
            else {
                botcheck();
            }
    }

    //Check userAgent to serve organic number to bots
     function botcheck() { 
            if(navigator.userAgent.toLowerCase().indexOf("googlebot") <= -1) {
                    document.write("default phone number");
                }
            else {
                    refcheck();
                }
    }

    //Check for referrer and serve number based on last referrer
    function refcheck() {
        var last_referrer = document.referrer;
        if (last_referrer.indexOf("google") > -1) {
            document.write("google tracking number");
        }
        else if (last_referrer.indexOf("yahoo") > -1) {
            document.write("yahoo tracking number");
        }
        else {
            document.write("default number");
        }
    }
</script>

Any help or guidance is very appreciated. I also need to set a session cookie to continue to serve the same number until a user leaves the site, but wanted to get this working first

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2024-10-21 15:37:45

您可以尝试这样的操作:

将此跨度放在页面上的某个位置:

Superdefaultnumber

然后将此代码放在页面头部的脚本标记中文档:

var setNumber = function(){
    var spanNode = document.getElementById("phoneNumber");
    if (!spanNode) {throw new Error("Unable to find span");}

    if (location.href.indexOf("?provider=") > -1) {
        spanNode.innerHTML = "paid phone number";
    }    

    //Check userAgent to serve organic number to bots
    if(navigator.userAgent.toLowerCase().indexOf("googlebot") <= -1) {
        spanNode.innerHTML = "default phone number";
    }

    //Check for referrer and serve number based on last referrer
    var last_referrer = document.referrer;
    if (last_referrer.indexOf("google") > -1) {
        spanNode.innerHTML = "google tracking number";
    }
    else if (last_referrer.indexOf("yahoo") > -1) {
        spanNode.innerHTML = "yahoo tracking number";
    }
    else {
        spanNode.innerHTML = "default number";
    }    
};


window.addEventListener("load", setNumber);

You could try something like this:

put this span somewhere on your page:

<span id="phoneNumber">Superdefaultnumber</span>

And then this code in a script tag in the head of your document:

var setNumber = function(){
    var spanNode = document.getElementById("phoneNumber");
    if (!spanNode) {throw new Error("Unable to find span");}

    if (location.href.indexOf("?provider=") > -1) {
        spanNode.innerHTML = "paid phone number";
    }    

    //Check userAgent to serve organic number to bots
    if(navigator.userAgent.toLowerCase().indexOf("googlebot") <= -1) {
        spanNode.innerHTML = "default phone number";
    }

    //Check for referrer and serve number based on last referrer
    var last_referrer = document.referrer;
    if (last_referrer.indexOf("google") > -1) {
        spanNode.innerHTML = "google tracking number";
    }
    else if (last_referrer.indexOf("yahoo") > -1) {
        spanNode.innerHTML = "yahoo tracking number";
    }
    else {
        spanNode.innerHTML = "default number";
    }    
};


window.addEventListener("load", setNumber);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文