matomo-org / matomo-org/tracker-proxy
Bulk request did not send `cip`
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 158
- Forks
- 46
- PR merge metrics
- No merged PRs in 30d
Description
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:
```php
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:
```php
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 :
```json
{
"requests": [
"?action=Page",
],
"send_image": 0
}
```
expected result:
```json
{
"requests": [
"?action=Page&cip=0.0.0.0",
],
"send_image": 0,
"token_auth": "TOKEN_AUTH"
}
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the POST handler that reads php://input and forwards tracker requests, then trace how bulk JSON requests are transformed before reaching Matomo 5. Reproduce a bulk request and verify that token_auth is included and cip reaches each individual query, while checking the listed media and form-tracking cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100