Giter VIP home page Giter VIP logo

sua-uach-conversion's Introduction

SUA to UA-CH Conversion Routines

Introduction

Structured User Agent or SUA for short has been introduced by IAB in OpenRTB v.2.6 spec. It contains the information passed as part of the User-Agent Client Hint HTTP headers, or that otherwise can be obtained from the NavigatorUAData.

The playground page is available here.

Rationale

Device detection services such as the 51Degrees Cloud Service or 51Degrees UAParser expect to receive a UA-CH header map as input. Thus one can't simply feed the OpenRTB's device.sua object into one of these. One first needs to convert it into the UA-CH header map format. This is where convertSUAtoUACH routine can be used.

Vice-versa, if you have received the UA-CH headers on the server as part of the (ad) request and would like to send them further as part of the OpenRTB request as device.sua, you first need to convert this header map into SUA JSON object representation. This justifies the use of the convertUACHtoSUA routine.

Usage

Converting SUA to UA-CH

To convert SUA to UA-CH HTTP header map representation, use the convertSUAtoUACH routine:

import convertSUAtoUACH from "./src/convertSUAtoUACH.js";

const SUAExample = {
  browsers: [
    { 
      brand:"Not A;Brand",
      version:["99","0","0","0"]
    },
    {
      brand: "Chromium", 
      version: ["99","0","4844","88"],
    },
    {
      brand: "Google Chrome",
      version: ["99","0","4844","88"]
    },
  ],
  platform: {
    brand: "Android",
    version: ["12"],
  },
  mobile: 1,
  architecture: "arm",
  bitness: "64",
  model: "Pixel 6",
  source: 2
}

let uach = convertSUAtoUACH(SUAExample)
console.log(uach)

and you would run this example like:

node example_sua_uach.js

Converting UA-CH to SUA

Here is an example of using the reverse convertUACHtoSUA routine:

import convertUACHtoSUA from "./src/convertUACHtoSUA.js";
const uach = {
  'sec-ch-ua-arch': '"x86"',
  'sec-ch-ua-bitness': '"64"',
  'sec-ch-ua-full-version-list': '"Google Chrome";v="111.0.5563.146", "Not(A:Brand";v="8.0.0.0", "Chromium";v="111.0.5563.146"',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-model':'',
  'sec-ch-ua-platform': '"macOS"',
  'sec-ch-ua-platform-version': '"13.3.0"'
}

let sua = convertUACHtoSUA(uach)
console.log(sua)

you can run this example like:

node example_uach_sua.js

Tests

You can run tests by executing:

yarn
yarn test

Found a bug? Please contribute either via pull request or an issue.

License

Copyright 2023 51Degrees.mobi Limited, Postindustria Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.