javascript 如何使用 js 和 php 创建跟踪脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5949496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:54:45  来源:igfitidea点击:

How to create a tracking script with js and php?

phpjavascript

提问by danidacar

I want to create a simple tracking script to give to my clients. Something similar with GA but very basic.

我想创建一个简单的跟踪脚本来提供给我的客户。与 GA 类似但非常基本的东西。

The requirements are

要求是

  • give the clients a single and simple js script like google Analytics does
  • 为客户提供一个简单的 js 脚本,就像 google Analytics 所做的那样
  • make most of the logic inside the js file loaded by 3th party sites from the main site
  • 使第 3 方站点从主站点加载的 js 文件中的大部分逻辑
  • collect in PHP the information and store it

    What I can't figure yet is what are the ways to do this? Google from what I see is loading a gif file, stores the information and parses the logs. If I do something similar sending the data to a php file Ajax cross site policy will stop me, from what I remember.

    So what is a clean way to do this ? ( I don't need code just the logic behind it )

  • 在 PHP 中收集信息并将其存储

    我还想不通的是有什么方法可以做到这一点?我所看到的谷歌正在加载一个 gif 文件,存储信息并解析日志。如果我做一些类似的事情,将数据发送到一个 php 文件,Ajax 跨站点策略会阻止我,我记得。

    那么有什么干净的方法可以做到这一点呢?(我不需要代码只需要它背后的逻辑)

  • 回答by mplungjan

    Method a - web bug:

    方法a - 网络错误:

    Give the user this:
    <img src="http://www.yourserver.com/yourtracking.php?associateid=3rdpartyid" width="1" height="1" />

    给用户这个:
    <img src="http://www.yourserver.com/yourtracking.php?associateid=3rdpartyid" width="1" height="1" />

    have the php return header("content-type:image/gif");and serve them a gif file for their effort.

    让 php 返回header("content-type:image/gif");并为他们提供一个 gif 文件。

    Method b - script

    方法 b - 脚本

    Create a php file that can parse parameters and have it return content-type:text/javascriptHave them load it like this: <script type="text/javascript" src="http://www.yourserver.com/yourtracking.php?associateid=3rdpartyid"></script>

    创建一个可以解析参数并返回的 php 文件让content-type:text/javascript他们像这样加载它: <script type="text/javascript" src="http://www.yourserver.com/yourtracking.php?associateid=3rdpartyid"></script>

    If you want to you can do additional stuff like

    如果你愿意,你可以做额外的事情,比如

    <script type="text/javascript">
      var associateId = "12345";
      var trackingPage="homepage";
    </script>
    <script type="text/javascript" src="http://www.yourserver.com/yourtracking.php?associateid=3rdpartyid"></script>
    

    then in the php have code like this (watch the nested quotes)

    然后在 php 中有这样的代码(观看嵌套的引号)

    $str = 'var url = "http://www.yourserver.com/moretracking.php?associateid="+associateId+';
    $str .= '"&page="+trackingPage+"&ref="+escape(document.referrer);\n';
    $str .= 'document.write(\'<img src="\'+url+\'"/>\');';
    
    echo $str;
    

    回答by rtheitroadodriguez

    You may read this (found googling) about cross domain ajax and its possible solutions... http://snook.ca/archives/javascript/cross_domain_aj/

    您可以阅读有关跨域 ajax 及其可能的解决方案的(找到谷歌搜索)... http://snook.ca/archives/javascript/cross_domain_aj/

    回答by Fredefl

    Well, i use some php code that is included in my script that logs Ip Addresses and as much information i can get from a server-side perspective. It saves it in a MySql Database. I also use a Ajax script to post data to a php script, the data in this case is Screen Heigth and thing you only can get client-side.

    好吧,我使用一些包含在我的脚本中的 php 代码来记录 Ip 地址以及我可以从服务器端的角度获得的尽可能多的信息。它将它保存在一个 MySql 数据库中。我还使用 Ajax 脚本将数据发布到 php 脚本,本例中的数据是 Screen Heigth 并且只能在客户端获取。