php - 如何在 php web 服务中包含谷歌分析

标签 php web-services google-analytics

我有一个用 php 编写的 Web 服务,它是从安装在 PC 上的桌面应用程序调用的。 我想要注册调用网络服务上的函数的用户,为此我只想将点击发送到 Google Analytics

PHP 中的网络服务:

<?php
   require_once('lib/nusoap.php'); // basic include.. must go at the top

   $SERVICE_NAMESPACE = "urn:Service"; // create a namespace to run under.

   $server = new soap_server(); // the soap object from the include above.

   // this has many input parameters but we only need two: the service name and the namespace
   $server->configureWSDL('Service', $SERVICE_NAMESPACE);


    $server->register('test',// method name
        array('name' => 'xsd:string', 'name99' => 'xsd:string'),// input parameter called name.. and it's a string.
        array('return' => 'xsd:string'),// output - one string is returned called "return"
        $SERVICE_NAMESPACE,// namespace
        $SERVICE_NAMESPACE . '#hello1',// soapaction
        'rpc',// style.. remote procedure call
        'encoded',// use of the call
        'Nada interesante'// documentation for people who hook into your service.
    );

    function test($sName,$sName99) 
    {           
        return 'TEST ';
    }

    //This processes the request and returns a result.
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA); 
?>

我想要谷歌分析信息,为此我想集成以下脚本:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-89356985-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments)};
  gtag('js', new Date());

  gtag('config', 'UA-89356985-1');
</script>

我不知道如何集成到测试功能中。我想知道用户何时调用测试函数。

非常感谢。

最佳答案

除非您的 PHP 脚本生成在浏览器中执行的 HTML 和 Javascript,否则您无法使用 Javascript 跟踪器。

但是您可以使用Measurement Protocol向 Google Analytics 进行服务器端调用。这基本上是一个端点,您可以在其中发送带有自定义值的预定义参数(通过任何可以进行 http 调用的方法),并且它们将在 Google Analytics 中注册。

关于php - 如何在 php web 服务中包含谷歌分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345864/

相关文章:

javascript - 当我选中一个或多个复选按钮时,如何在联系表单 7 上启用 Google 分析跟踪?

javascript - 如何更新数据层变量?

php - 如何使用 Google API PHP 客户端库和 Youtube API V3 将视频上传到 YouTube?

session - session id 太长或包含非法字符,有效字符为 a-z、A-Z、0-9 和 '-,'

java - SoapFault - 故障代码 : 'soap:Server' faultstring: 'Server was unable to process request. 未将对象引用设置为对象的实例

javascript - 将数组中的值插入到基于键的 SOAP 消息中

php - preg_match 获取包含 123 和 .txt 之间任何字符的文件?

php - 我如何使用 php 或 javascript 或 jquery 制作包含图像和文本的 html div 的屏幕截图?

使用 jQuery UI Sortable 序列化方法的 ASP.NET Web 服务示例

javascript - 如何将转化数据与 Google 广告一起发送到 Google Analytics