dongpu5874 2017-11-17 08:00
浏览 94
已采纳

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

I have a webservice writen in php and it is called from an desktop application installed on PC's. I want to have a register of the users who calls the functions on the web service and for this I only want to send hits to Google Analytics.

webservice in 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); 
?>

I want to have google analytics info and for that i want to integrate the following script:

<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>

I don't know how to integrate in the test function. I want to know when the users calls the test function.

Thanks in advance very much.

  • 写回答

3条回答 默认 最新

  • doucan4815 2017-11-17 08:13
    关注

    You cannot use the Javascript tracker unless your PHP script produces HTML and Javascript that is executed in a browser.

    You can however use the Measurement Protocol to make server side calls to Google Analytics. That's basically an endpoint where you send predefined parameters with your custom values (via any method that can make http calls) and they will register in Google Analytics.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?