voicerecordist
voicerecordist

Reputation: 61

Argument 1 passed to UAParser\Parser::parse() must be of the type string, null given in laravel stats tracker

I installed this package but when I running php artisan migrate, I see this error:

In Parser.php line 35:

Argument 1 passed to UAParser\Parser::parse() must be of the type string, null given, called in...

my code in config/database.php:

'tracker' => [
            'driver'   => 'tracker',
            'host'     => env('DB_HOST', '127.0.0.1'),
            'database' => env('DB_DATABASE', 'forge'),
            'strict' => false,    // to avoid problems on some MySQL installs
],

what is the problem?

I think the above code must be wrong.

Upvotes: 1

Views: 5010

Answers (3)

sudin
sudin

Reputation: 332

Search for this file pragmarx\tracker\src\Support\UserAgentParser.php and add the line below at the start of construct method.

$userAgent = $_SERVER['HTTP_USER_AGENT'];

Upvotes: 0

dikutandi
dikutandi

Reputation: 127

I think there is an issue in the package that needs fixing to be able to work. The error occurs when composer dump-autoload runs.

So I edited the pragmarx\tracker\src\Support\UserAgentParser.php and edit the construct method.

Here is my code:

public function __construct($basePath, $userAgent = null)
    {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        if (!$userAgent && isset($_SERVER['HTTP_USER_AGENT'])) {
            $userAgent = $_SERVER['HTTP_USER_AGENT'];
        }

        $this->parser = Parser::create()->parse($userAgent);

        $this->userAgent = $this->parser->ua;

        $this->operatingSystem = $this->parser->os;

        $this->device = $this->parser->device;

        $this->basePath = $basePath;

        $this->originalUserAgent = $this->parser->originalUserAgent;
    }

After that, I run composer dump-autoload my self.

NOTE: Of course when you run composer update or composer install this error occur again

Upvotes: 1

Christophe Hubert
Christophe Hubert

Reputation: 2951

There is an issue in the package that needs fixing to be able to work. A quick workaround would be to set the user_agent manually by php if absent:

You can add the following at the beginning of your public\index.php temporarily:

if (!isset($_SERVER['HTTP_USER_AGENT']))
    ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');

Upvotes: 1

Related Questions