# Geolocation

This guide explains how to detect a user’s country and decide when AgeGO should be triggered based on geographic and regulatory requirements.

The solution supports:

* One country
* Multiple countries
* A clear fallback logic when detection fails

## <mark style="color:$primary;">Overview</mark>

AgeGO can be triggered only for users coming from specific countries, while users from other countries can access the content directly.

This setup allows you to:

* Enforce age verification only where legally required
* Reduce friction in countries where age verification is not mandatory
* Use a single implementation for multiple geographies

You define:

* Which country or countries require AgeGO
* Which AgeGO `asi` should be used for your domain

## <mark style="color:$primary;">Prerequisites</mark>

Before implementing this logic, make sure you have:

* An active AgeGO account
* Your AgeGO `asi` (alpha-numeric)
* A country detection method, such as:
  * Server-side geolocation
  * Client-side IP-based country detection
  * A CMP or analytics tool providing country code

Country codes must follow **ISO 3166-1 alpha-2 format**, for example:

* IT (Italy)
* FR (France)
* ES (Spain)
* DE (Germany)

Hide content of the web page:

```routeros
<div id="general" style="display:none;">
```

## <mark style="color:$primary;">Implementation / SetUp</mark>

#### <mark style="color:$info;">**HTML Structure**</mark>

Wrap all content that should be gated by logic inside `#general` and keep it hidden initially.

```xml
<div id="general" style="display:none;"> //hides content <section class="section bg-light">   <div class="container">     <h2>Ad Section</h2>     <p class="small">Banner</p>     <script async type="application/javascript" src="https://test.example.com/ad-provider.js"></script>     <ins class="eas6a97888e2" data-zoneid="5759198"></ins>     <script>(AdProvider = window.AdProvider || []).push({ "serve": {} });</script>   </div>
```

#### <mark style="color:$info;">**Javascript Disable Fallback**</mark>

Redirect users with disabled JavaScript.

```xml
<!-- ...existing code... --><noscript> <meta http-equiv="refresh" content="0; url=https://myapi.agego.com/noJS" /></noscript><!-- ...existing code... -->
```

#### <mark style="color:$info;">**Country Detection and Conditional Logic**</mark>

```xml
<script>async function loadCountryLogic() { let countryCode = localStorage.getItem('userCountry');  if (!countryCode) {   try {     const res = await fetch('https://get.geojs.io/v1/ip/country.json');     const data = await res.json();     countryCode = data.country || 'unknown';     localStorage.setItem('userCountry', countryCode);   } catch (err) {     console.warn('GeoJS API failed, trying fallback...');     try {       const res2 = await fetch('https://ipapi.co/json/');       const data2 = await res2.json();       countryCode = data2.country_code || 'unknown';       localStorage.setItem('userCountry', countryCode);     } catch (err2) {       console.warn('All geo APIs failed, using browser language.');       const lang = navigator.language || navigator.userLanguage;       countryCode = (lang && lang.includes('-')) ? lang.split('-')[1] : 'unknown';     }   } }  console.log('Detected country:', countryCode);  if (countryCode === 'IT' ) {               // Example for multiple countries:                                                               const targetCountries = ['IT', 'FR', 'DE', 'ES', 'GB'];                                                                   if (targetCountries.includes(countryCode)) {    runAgeGoVerification(countryCode);} else {     showAdsDirectly(countryCode);}   function runAgeGoVerification(countryCode) {   const general = document.getElementById('general');   if (general) general.style.display = 'block';    const script = document.createElement('script');   script.src = "https://verifycdn.agego.com/v1/verify.js";   script.onload = function () {     window.AGEGO = window.AGEGO || function () { (AGEGO.e = AGEGO.e || []).push(arguments) };     AGEGO('configure', {       asi: cbd1abb5-ab8c-22f0-9bef-525400fgr7da, // replace with your AgeGO asi       autoBlur: true,       verifyMode: 'inline',       requireAgeVerification: true,       allowDirectContinue: false,       underageRedirectTo: 'https://google.com',       overlay: {         logo: '',   // Add a link to your logo image to be shown in AgeGO widget         theme: 'auto'       }     });   };   document.head.appendChild(script); }  function showAdsDirectly(countryCode) {   const general = document.getElementById('general');   if (general) general.style.display = 'block'; }} loadCountryLogic();</script>
```

## <mark style="color:$primary;">**Recommended Geolocation Priority**</mark>

If you do not already have a country detection solution, we recommend using the following priority:

1. IP-based geolocation via geojs.io (primary)
2. IP-based geolocation via ipapi.co (fallback)
3. Browser language (last fallback only)

The first valid country code detected should be used.

**Note:** Browser language is less reliable and should only be used when IP-based methods are not available.

## <mark style="color:$primary;">Important Notes About</mark> <mark style="color:$primary;"></mark><mark style="color:$primary;">`asi`</mark>

1. The asi is an alpha-numeric identifier provided by AgeGO
2. It identifies your domain and configuration
3. It is not related to the country

**Note:**

**✔** The same site ID can be used for one or multiple countries.

**❌** Country code and site ID are not the same thing.

## <mark style="color:$primary;">Testing the Integration</mark>

You can simulate different countries during testing in Google Chrome DevTools - Console.

Simulate an enforced country

```javascript
localStorage.setItem('userCountry', 'IT');location.reload();
```

Simulate a non-enforced country

```javascript
localStorage.setItem('userCountry', 'US');location.reload();
```

Reset the browser's memory

```jboss-cli
localStorage.clear();location.reload();
```

## <mark style="color:$primary;">Key Notes & Best Practices</mark>

* Always use ISO country codes
* Keep country enforcement configurable, not hardcoded
* Use comma-separated values for scalability
* Make sure the same site ID is used consistently per domain
* Country detection should happen before loading AgeGO

## <mark style="color:$primary;">Summary</mark>

* AgeGO can be conditionally enforced by country
* One or multiple countries are supported
* Configuration is simple and scalable
* The same logic works for all markets

This approach allows you to remain compliant while maintaining the best possible user experience.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.agego.com/integration/geolocation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
