简体   繁体   中英

adding mobile browser detection, rule selection, into a ruleset

I would like to add functionality to a ruleset that fires a distinct rule based on whether or not the browser is mobile or not. (one rule fires for a standard browser, a different rule fires for a mobile browser) I know that the browser detection can be done any number of ways, but my first inclination would be with javascript.

Any thoughts on how to start with this?

You can use the useragent object , like this:

rule detect_agent {
    select when pageview ".*"
    pre {
        browser_name = useragent:browser_name();
        browser_version = useragent:browser_version();
        os = useragent:os();
        os_type = useragent:os_type();
        os_version = useragent:os_version();
        full_useragent = useragent:string();
        message = <<
            <p><strong>Information about your browser:</strong></br />
            <em>Browser name:</em> #{browser_name}</br />
            <em>Browser version:</em> #{browser_version}</br />
            <em>Operating system:</em> #{os}</br />
            <em>OS type:</em> #{os_type}</br />
            <em>OS version:</em> #{os_version}</br /></p>
            <p>#{full_useragent}</p>
        >>;
    }
    append("body", message);
}

You might have to do some parsing of your own, though, since the browser_name and os may or may not be correct. Here's what it looks like in Chrome on a Mac (you can test it using this URL in any browser):

Chrome,Mac

Here's what it looks like in Safari on an iPad:

Safari,iPad

Do some research into what the UserAgent strings look like for the browsers you care about. Then you can use the useragent:string() function together with match() to determine what to do with it. (If you want an example of how to do that, let me know.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM