PHPackages                             marknotton/agent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. marknotton/agent

ActiveCraft-plugin[Utility &amp; Helpers](/categories/utility)

marknotton/agent
================

An extension to Jens Segers Agent tool for querying user agent data.

5.0.0(2y ago)726.8k↓33.3%4[2 issues](https://github.com/marknotton/craft-plugin-agent/issues)MITPHPPHP ^7.2.5|^8.0CI failing

Since Mar 9Pushed 2y ago1 watchersCompare

[ Source](https://github.com/marknotton/craft-plugin-agent)[ Packagist](https://packagist.org/packages/marknotton/agent)[ RSS](/packages/marknotton-agent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (20)Used By (0)

[![Agent: An extension to Jens Segers Agent tool for querying user agent data.](https://camo.githubusercontent.com/895a68a1e2ec3e0cef3a535f13c827a2a44a93b990b3e61b4d568c73c4a33658/68747470733a2f2f692e696d6775722e636f6d2f754b32466e6e552e6a7067)](https://camo.githubusercontent.com/895a68a1e2ec3e0cef3a535f13c827a2a44a93b990b3e61b4d568c73c4a33658/68747470733a2f2f692e696d6775722e636f6d2f754b32466e6e552e6a7067)

Installation
------------

[](#installation)

```
composer require marknotton/agent

```

Official Documentation
----------------------

[](#official-documentation)

This really is just an extension to Jens Segers Agent utility. Refer to their [documentation](https://github.com/jenssegers/agent) for all available methods.

Whilst I have tried to keep this plugin as lean as possible, I have extended Jens Segers Agent utility by including a couple of my own methods...

### Check

[](#check)

Check to determine the users browser and version type is a match.

##### Example 1:

[](#example-1)

`true` if the users current browser matches either browser name

```
{{ craft.agent.check('edge', 'firefox') }}
```

##### Example 2:

[](#example-2)

You can use most [comparison operators](https://www.php.net/manual/en/language.operators.comparison.php) to match against the browsers version.

`true` if browser version is equal to 100:

```
{{ craft.agent.check('chrome == 100'}}
```

`true` if browser version is not equal to 100:

```
{{ craft.agent.check('chrome != 100'}}
```

`true` if browser version is less than 100:

```
{{ craft.agent.check('chrome < 100'}}
```

`true` if browser version is greater than 100:

```
{{ craft.agent.check('chrome > 100'}}
```

`true` if browser version is less than or equal to 100:

```
{{ craft.agent.check('chrome = 100'}}
```

##### Example 3:

[](#example-3)

You can add multiple criteria for your check. `true` if any criteria is a match:

```
{{ craft.agent.check('ie 9', 'chrome > 49', 'firefox') }}
```

##### Example 5:

[](#example-5)

You may also negate a check by prefixing a `not ` string. `true` if the users current browser is not IE version 9 or above.

```
{{ craft.agent.check('not ie => 9') }}
```

### User agent whitelist:

[](#user-agent-whitelist)

If the User Agent contains any whitelist exceptions, even with partial matches, then the Check method will always return `true`. This can be useful for allowing certain bots to pass the Check method.

You can mange the whitelist by creating an `agent.php` config file in your projects `configs` directory:

```
return [
'userAgentWhitelist' => [
	'APIs-Google',
	'Mediapartners-Google',
	'AdsBot-Google',
	'Googlebot-Image',
	'Googlebot',
	'FeedFetcher-Google'
	]
];
```

or via the CMS plugin settings:

[![User Agent Whitelist](https://camo.githubusercontent.com/1d196c9e2c24ac6bdff3978cd7a998d2c7ddb430f105075a023ab685411161c0/68747470733a2f2f692e696d6775722e636f6d2f53756f746668762e706e67)](https://camo.githubusercontent.com/1d196c9e2c24ac6bdff3978cd7a998d2c7ddb430f105075a023ab685411161c0/68747470733a2f2f692e696d6775722e636f6d2f53756f746668762e706e67)

### Version

[](#version)

Jens Segers original **version** method required a property name (browser, platform, os, etc...); and the return value would resolve to a full schema version:

```
{{ craft.agent.version(craft.agent.browser) }} // 104.3.0.1
```

I have found in most cases getting the major browser version would suffice. So instead of the previous example you can return a 'floored' version number where the browser is the assumed default argument.

```
{{ craft.agent.version() }} // 104
```

You can still get full version or a floated version number like so:

```
{{ craft.agent.version('text') }} // 104.3.0.1
{{ craft.agent.version('float') }} // 104.3
```

### Redirect

[](#redirect)

Redirect users to a new template/url if the user agent doesn't match any of the check method criteria:

```
{% set criteria = [
  'chrome > 55',
  'firefox > 44',
  'safari >= 7',
  'edge >= 15',
  'opera > 50'
] %}

{{ craft.agent.redirect(criteria, 'no-support.twig', 302) }}
```

### Data

[](#data)

To set the user agents device name, version and device type directly to an element. Use Crafts [attr method](https://craftcms.com/docs/4.x/dev/functions.html#attr) to render common data attributes.

```

```

The end result will look like something like this:

```

```

*But why would you want this?* This opens up some options for browser specific styling within your CSS; and this server side approach will omit flashes of unstyled content (FOUC) or layout shifts because styling rules aren't dependant on Javascript during page load. This means you can confidently use something like this in your CSS:

```
html[data-browser-name="safari"] article img { ... }
```

agent.js
--------

[](#agentjs)

There is a small [IIFE](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression) agent.min.js (&lt; 0.7k) file that can be injected directly into the ``. You'll need to enable this via the plugin settings.

[![Agent CMS Toggle Option](https://camo.githubusercontent.com/9a9525503b195946bbf958d7ce19c3664d957d81d64d11073cff56855bd23126/68747470733a2f2f692e696d6775722e636f6d2f7a375139596e6c2e706e67)](https://camo.githubusercontent.com/9a9525503b195946bbf958d7ce19c3664d957d81d64d11073cff56855bd23126/68747470733a2f2f692e696d6775722e636f6d2f7a375139596e6c2e706e67)

This will define global properties to the window object for the browser name, version, and different device types.

`window.browser.name`string`window.browser.version`int`window.device`string`window.isPhone`bool`window.isTable`bool`window.isDesktop`boolAlternatively you can register the the agent.min.js asset manually:

Twig:

```
{{ craft.agent.registerAgentJsFile(, ) }}
```

Php:

```
Agent::registerAgentJsFile(, );
```

Change Log &amp; Breaking Changes
---------------------------------

[](#change-log--breaking-changes)

There have been many changes since the previous version of Agent 1.2.0. Some for performance, some for sanity. Arguably some practices used in the previous version were over engineered for no obvious gains. These changes could be breaking, that require small syntax tweaks to resolve on older projects. [Please review the "4.0.0 - 2022-09-11" change log for suggestions and fixes.](https://github.com/marknotton/craft-plugin-agent/blob/master/CHANGELOG.md#400---2022-09-11)

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance14

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~121 days

Recently: every ~343 days

Total

19

Last Release

814d ago

Major Versions

1.2.0 → 4.0.02022-08-21

v4.x-dev → 5.0.02024-02-24

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd5781885228b82774405eb88c56731501bd53b3886a023018f8d2d867571fca?d=identicon)[marknotton](/maintainers/marknotton)

---

Top Contributors

[![marknotton](https://avatars.githubusercontent.com/u/4622921?v=4)](https://github.com/marknotton "marknotton (21 commits)")[![angrybrad](https://avatars.githubusercontent.com/u/61869?v=4)](https://github.com/angrybrad "angrybrad (1 commits)")[![marknotton-yello](https://avatars.githubusercontent.com/u/153639233?v=4)](https://github.com/marknotton-yello "marknotton-yello (1 commits)")[![zizther](https://avatars.githubusercontent.com/u/1436378?v=4)](https://github.com/zizther "zizther (1 commits)")

### Embed Badge

![Health badge](/badges/marknotton-agent/health.svg)

```
[![Health](https://phpackages.com/badges/marknotton-agent/health.svg)](https://phpackages.com/packages/marknotton-agent)
```

###  Alternatives

[spicyweb/craft-neo

A Matrix-like field type with block hierarchy

395798.1k10](/packages/spicyweb-craft-neo)[verbb/formie

The most user-friendly forms plugin for Craft.

101372.9k40](/packages/verbb-formie)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

52664.9k12](/packages/solspace-craft-freeform)[supercool/tablemaker

Create customizable and user-defined table fields.

40141.7k](/packages/supercool-tablemaker)[verbb/vizy

A flexible visual editor field for Craft.

4348.6k](/packages/verbb-vizy)[verbb/social-poster

Automatically post entries to social media.

918.5k](/packages/verbb-social-poster)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
