> ## Documentation Index
> Fetch the complete documentation index at: https://api.globalwebindex.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Compare Results by Location

Understanding how audience behaviour varies by location is essential for data-driven decision-making. The
`/v2/query/stats` endpoint enables users to compare survey responses across different regions by applying a splitter for
location-based questions. This allows for side-by-side analysis of key metrics, such as audience percentage, across
multiple countries or markets.

The sidebar examples compare Facebook usage between USA (`s2_1`) and Germany (`s2_49`). To achieve
that, you can set a "splitter" to the locations question `s2` and "segments" to the two locations. You can also use
locations filter to specify these two locations to get more specific datapoint metrics in the result, see **Example A**.

The most relevant metric seems to be the `audience_percantage`. Let's say you would like to compare this percentage of
respondents who use Facebook at least once a day between locations. If you save the **Request A** JSON body above as
`req.json`, you can run the provided Python script example.

You can freely adjust the `req.json` to examine a different question asked in different waves, and in the
script provide a list of all locations you'd like to include in your comparison.

Furthermore, you could narrow down the result with an Audience, too. Say you have an Audience of people who like gaming
(`q3181c_23`), aged 25-34 (`q4_3`), and you'd like to compare the numbers of daily Facebook users within this Audience
between the USA and Germany. (The Audience expression will be shorter than what was in the
[previous section](create-audiences).)

<RequestExample>
  ```json Request A theme={null}
  {
    "question": "q42011a",
    "datapoints": [
      "q42011a_3"
    ],
    "suffixes": [
      1,
      2
    ],
    "locations": [
      "s2_1",
      "s2_49"
    ],
    "waves": [
      "q4_2022"
    ],
    "splitter": "s2",
    "segments": [
      "s2_1",
      "s2_49"
    ]
  }
  ```

  ```python Python Script theme={null}
  import json
  import requests

  with open('req.json', 'r') as file:
      json_data = json.load(file)

  # List of locations to replace as locations filter and segments in the JSON object (USA, Germany)
  locations = ["s2_1", "s2_49"]
  json_data["locations"] = locations
  json_data["segments"] = locations

  url = "https://api.globalwebindex.com/v2/query/stats"
  headers = {"Authorization": "Bearer YOUR_API_KEY"}
  response = requests.post(url, json=json_data, headers=headers)
  response.raise_for_status()
  data = response.json()

  # Process the response to get audience percentages identified by the location (segment) string
  audience_percentages = []
  for item in data["data"]:
      segment = item["segment"]
      audience_percentage = item["metrics"]["audience_percentage"]
      audience_percentages.append((segment, audience_percentage))

  for location, percentage in audience_percentages:
      print(f"Location: {location}, Audience Percentage: {percentage}")

  max_location, max_percentage = max(audience_percentages, key=lambda x: x[1])
  print(f"Location with highest audience percentage: {max_location} ({max_percentage}%)")
  ```

  ```json Request B theme={null}
  {
    "audiences": [
      {
        "name": "Gamers 25-34",
        "expression": {
          "and": [
            {
              "question": "q4",
              "datapoints": [
                "q4_3"
              ]
            },
            {
              "question": "q3181c",
              "datapoints": [
                "q3181c_23"
              ]
            }
          ]
        }
      }
    ],
    "question": "q42011a",
    "datapoints": [
      "q42011a_3"
    ],
    "suffixes": [
      1,
      2
    ],
    "locations": [
      "s2_1",
      "s2_49"
    ],
    "waves": [
      "q4_2022"
    ],
    "splitter": "s2",
    "segments": [
      "s2_1",
      "s2_49"
    ]
  }
  ```
</RequestExample>

<ResponseExample>
  ```json Response A theme={null}
  {
    "meta": {
      "type": "DATAPOINTS-SEGMENTS",
      "question": "q42011a",
      "splitter": "s2"
    },
    "data": [
      {
        "datapoint": "q42011a_3",
        "waves": [
          "q4_2022"
        ],
        "metrics": {
          "positive_sample": 15205,
          "positive_size": 117655215,
          "audience_sample": 25739,
          "audience_size": 205843910,
          "audience_percentage": 57.2,
          "audience_index": 104.7,
          "datapoint_sample": 20012,
          "datapoint_size": 140532912,
          "datapoint_percentage": 83.7
        },
        "suffixes": [
          1,
          2
        ],
        "segment": "s2_1"
      },
      {
        "datapoint": "q42011a_3",
        "waves": [
          "q4_2022"
        ],
        "metrics": {
          "positive_sample": 4807,
          "positive_size": 22877696,
          "audience_sample": 10828,
          "audience_size": 51688228,
          "audience_percentage": 44.3,
          "audience_index": 81.1,
          "datapoint_sample": 20012,
          "datapoint_size": 140532912,
          "datapoint_percentage": 16.3
        },
        "suffixes": [
          1,
          2
        ],
        "segment": "s2_49"
      }
    ]
  }
  ```

  ```plaintext Script Result A theme={null}
  Location: s2_1, Audience Percentage: 57.2
  Location: s2_49, Audience Percentage: 44.3
  Location with highest audience percentage: s2_1 (57.2%)
  ```

  ```plaintext Script Result B theme={null}
  Location: s2_1, Audience Percentage: 60.6
  Location: s2_49, Audience Percentage: 48.2
  Location with highest audience percentage: s2_1 (60.6%)
  ```
</ResponseExample>
