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

How to add custom app user agent without polluting core? #5931

Closed
klodha opened this issue Mar 1, 2019 · 2 comments
Closed

How to add custom app user agent without polluting core? #5931

klodha opened this issue Mar 1, 2019 · 2 comments

Comments

@klodha
Copy link

klodha commented Mar 1, 2019

Hi,

I found this wonderful library while searching for handling user-agent based results in my web application. It worked out of the box for all major browser platforms.

I have a specific requirement to track native application along with web access. This device detector library works pretty well for all popular/public user agents. However I would like to add my custom application for example MyAppName iOS 1.0 and MyAppName Android 1.0 to the list.

I started with creating a custom parser extending ClientParserAbstract and setting custom yml file to fixture.

class MyAppParser extends ClientParserAbstract
{
    protected $fixtureFile = 'custom_apps.yml';
    protected $parserName = 'custom_app';
    
}

Then in my application after creating object of DeviceDetector, I called addClientParser method with a new object of my parser class before calling parse() method.

So far no error or warning, and after parse I can use getOs fine. But calling getClient returns a warning Illegal string offset 'regex' in vendor/piwik/device-detector/Parser/ParserAbstract.php on line 260.

Just for trial, I had added same signature in regexes/client/mobile_apps.yml file and it worked exactly how I wanted. But I don't want to modify original regex dataset with my custom signatures.

So either I am missing something or this is not right way to extend library for custom application signatures. Any clue will be highly appreciated.

@Spikes042
Copy link
Contributor

You need to override getRegexesDirectory() as it will always return the vendor package directory

protected function getRegexesDirectory(){
    return __DIR__;
}

Else this line (https://github.com/matomo-org/device-detector/blob/master/Parser/ParserAbstract.php#L155) fails to find your .yml file

You should also extend the core DeviceDetector package and override addClientParser() to push the new ClientParser in front of the array rather than behind, else the Browser parser will match the UserAgent before it even gets to your Parser.
Something like this:

<?php

namespace MyApp\DeviceDetector;

use function array_pop;
use function array_unshift;

class DeviceDetector extends \DeviceDetector\DeviceDetector{
    public function addClientParser($parser){
        parent::addClientParser($parser);

        $item = array_pop($this->clientParsers);
        array_unshift($this->clientParsers, $item);
    }
}

@whimsicaldreamer
Copy link

@klodha Have you solved the issue? If so could, you post here, what you did? I have the same exact requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants