Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk request did not send cip #86

Open
ducdigital opened this issue Jan 16, 2024 · 0 comments
Open

Bulk request did not send cip #86

ducdigital opened this issue Jan 16, 2024 · 0 comments

Comments

@ducdigital
Copy link

ducdigital commented Jan 16, 2024

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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant