AMP页面上的client_id

时间:2018-10-17 13:41:28

标签: php google-analytics lumen amp-html clientid

我一直在使用simoahava和DanWilkerson方法来跟踪AMP页。我知道client_id在放大器与非放大器上存在问题,如此处所述:https://www.napkyn.com/2017/11/23/how-to-use-the-amp-client-id-api-for-consistent-user-tracking-in-google-analytics/

我一直在遵循指南,但仍然看不到自己在做什么错。我有两个问题:

  1. 当我从主域转到amp托管域时,来自主域的cookie会自动插入到amp托管域中,但我没有设置执行此操作,因此我不知道为什么主域中的cookie可用于amp托管域。 amp托管域不在Google中,而是在另一台服务器(Google云平台)上。那么为什么主页上的cookie会显示在我的另一个amp项目站点中?
  2. 未拾取client_id,并且已将amp-xxxx client_id与来自主域的GA.xxx一起设置。

主域: https://www.proteinfabrikken.no/

AMP页面: https://amp-proteinfabrikken-dot-hydra-connect.appspot.com/amp/proteinfabrikken/product/bcaa-211-400g

这是我的HTML代码的样子:

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>

和端点:

<amp-analytics config="https://amp-proteinfabrikken-dot-hydra-connect.appspot.com/amp/proteinfabrikken/gtm-analytics.config.json" type="googleanalytics" data-credentials="include"></amp-analytics>

现在我的服务器中的路由指向/gtm-analytics.config.json:

// AMP TESTING
$app->get($prefix . '/gtm-analytics.config.json', function (Request $request, Response $response) {


    // Generate random Client ID
    function generate_ga_client_id() {
        return rand(100000000, 999999999) . '.' . time();
    }

    // Set cookie to expire in 2 years
    function getCookieExpirationDate() {
        return date('D, j F Y H:i:s', time() + 60 * 60 * 24 * 365 * 2);
    }

    // Callback for the GET request
    function retrieve_gtm_json($data) {
        /* Get the hostname of the request origin, and parse it for the
         * pure domain name. */




        $domainName = str_replace('www.', '', 'https://www.amp-proteinfabrikken-dot-hydra-connect.appspot.com/amp/proteinfabrikken/product/bcaa-211-400g');
        syslog(LOG_INFO, "DOMAIN_NAME:" . $domainName);

        // Get the number of parts in the domain name
        $domainLength = count(explode('.', $domainName));
        syslog(LOG_INFO, "DOMAIN_LENGTH:" . $domainLength);
        /* Check if the browser already has the _ga cookie.
            * If not, generate a new cookie. */

        syslog(LOG_INFO, "HVA ER INNE I _GA:". $_COOKIE['_ga']);
        if (isset($_COOKIE['_ga'])) {
            $cid = $_COOKIE['_ga'];
            syslog(LOG_INFO, "ISSET COOKIE: TRUE:");
            syslog(LOG_INFO, "ISSET COOKIE: TRUE. Cookie:".$_COOKIE['_ga']);
        } else {

            $cid = "GA1.{$domainLength}." . generate_ga_client_id();
            syslog(LOG_INFO, "ISSET COOKIE: FALSE:");
            syslog(LOG_INFO, "ISSET COOKIE: FALSE. Cookie:".$cid);
        }


        syslog(LOG_INFO, "COOKIE:" . $cid);
        /* Store the actual Client ID (last two numbers) of the
         * _ga cookie value in the $cidNumber variable */
        $cidNumber = preg_replace('/^GA.\.[^.]+\./', '', $cid);
        syslog(LOG_INFO, "Client_ID_number:" . $cidNumber);


        /* Fetch the actual GTM container, by passing the valid query parameters from
         * the original request. */
        $container = file_get_contents("https://www.googletagmanager.com/amp.json?id=GTM-MFWM3QP&gtm.url=https://amp-proteinfabrikken-dot-hydra-connect.appspot.com/amp/proteinfabrikken/product/bcaa-211-400g");


        // Replace the CLIENT_ID(AMP_ECID_GOOGLE) string with ${clientId}
        $container = str_replace('CLIENT_ID(AMP_ECID_GOOGLE)', '${clientId}', $container);

        // Add the clientId to the "vars" object in the container JSON.
        $container = json_decode($container);
        $container->vars->clientId = $cidNumber;


        //JSON Encode pga feil på lumen To_String()
        $contained = json_encode($container);
        syslog(LOG_INFO, "GTM_CONTAINER:" . $contained);


        $response = response($contained, 200)
            ->header('Content-Type', 'text/plain')
            // Add the required headers (Set-Cookie, most importantly) to the Request
            ->header('Set-Cookie', "_ga={$cid}; Path=/; Expires=" . getcookieExpirationDate() . " GMT; Domain=https://www.proteinfabrikken.no;")
            ->header('Access-Control-Allow-Origin', 'https://cdn.ampproject.org')
            ->header('Access-Control-Allow-Credentials', 'true')
            // Remember to check the protocol and change to http if that's where your domain is
            ->header('AMP-Access-Control-Allow-Source-Origin', "https://www.proteinfabrikken.no")
            ->header('Access-Control-Expose-Headers', 'AMP-Access-Control-Allow-Source-Origin');
        // Return the HTTP response.


        return $response;


    }


    return retrieve_gtm_json('https://amp-proteinfabrikken-dot-hydra-connect.appspot.com/amp/proteinfabrikken/product/bcaa-211-400g');


});

我正在使用流明。任何人都可以帮助解决这个问题,也许以前遇到过这个问题?谢谢!

0 个答案:

没有答案