tracker-proxy icon indicating copy to clipboard operation
tracker-proxy copied to clipboard

Bulk request did not send `cip`

Open ducdigital opened this issue 2 years ago • 0 comments

It seems like tracker-proxy does not include token_auth and cip in bulk request . The X-Forwarded-For does not work with bulk requests on Matomo 5.

For all bulk requests, the server IP is used on Matomo instead of the real forwarded IP address.

I took some liberty to add a few more methods to the file, it works but I did not test it for PHP < 7.4.

What works:

  • Custom events
  • Media tracking (Play)

What seems to be broken:

  • Media tracking (seeking). (Could it be that the media tracking plugin does not take into account the cip passed in the bulk request?)
  • Form interaction ( doesn't seem to track, not sure why. I will post here if it works again )

Here's the code:

function isUsingBulkRequest($rawData)
{
    if (!empty($rawData)) {
        return strpos($rawData, '"requests"') || strpos($rawData, "'requests'");
    }

    return false;
}

function processBulkQuery($query, $extraParam = []) {
    $cleanedQuery = $query;

    if (substr($query, 0, 1)) {
        $cleanedQuery = substr($query, 1);
    }

    $parsedQuery = array();
    $parsedUrl = parse_str($cleanedQuery, $parsedQuery);

    return '?' . http_build_query(array_merge($parsedQuery, $extraParam));
}

function buildAuthBulkRequest($rawData, $auth_token, $cip) {
    $jsonData = json_decode($rawData, $assoc = true);
    if (!isset($jsonData['requests'])) {
        return $rawData;
    }
    $jsonData['token_auth'] = $auth_token;

    $extraQueryParams = [
        "cip" => $cip,
    ];

    $jsonData['requests'] = array_map(
        function($query) use ($extraQueryParams) {
            return processBulkQuery($query, $extraQueryParams);
        },
        $jsonData['requests']
    );

    return json_encode($jsonData);
}

Add the following code in the POST handler will add cip to the individual query and token_auth on the post body:

        global $TOKEN_AUTH;
        $postBody = file_get_contents("php://input");
        $isBulk = isUsingBulkRequest($postBody);
...
        if($isBulk) {
            $stream_options['http']['content'] = buildAuthBulkRequest($postBody, $TOKEN_AUTH, $visitIp);
        }
        $stream_options['http']['header'][] = "Content-Length: " . strlen($postBody);

original requests :

{
    "requests": [
        "?action=Page",
    ],
    "send_image": 0
}

expected result:

{
    "requests": [
        "?action=Page&cip=0.0.0.0",
    ],
    "send_image": 0,
    "token_auth": "TOKEN_AUTH"
}

ducdigital avatar Jan 16 '24 00:01 ducdigital