[Condition] Cookie-based GeoLocalization in Oxygen

le 26/10/2021
JAG

In a recent project I completed, the requirements were
1) to show flags of certain South American countries on the splash page (site.com).

When the visitor clicks Brasil or Argentina or Espana, he/she should be taken to the corresponding country “homepage” i.e., site.com/brasil or site.com/argentina or site.com/espana.
Clicking on any of the other countries should take them to site.com/home.
2) to be able to output elements based on
selected country or not the selected countrywhether the selected country is Brasil/Argentina/Espana or not
This can be done by
1) creating a cookie named say, actioncoach_visitorcountry and setting its value equal to the selected country using JS
Visitor selecting a country
Cookie set to the selected country
2) creating a custom Visitor Country condition that returns true or false depending on the value in the cookie
thereby allowing the site owner to output any element per the requirements.
Applying the Visitor Country condition. Available operators: = and !=
This members-only tutorial provides the steps covering how I’ve set up the above. You may want to treat this as a general guide, try to understand how the code works, and implement it whilst making any necessary changes per your project’s requirements.
Warning: Cookie-based approach will not work with Varnish or other type of caching in place. You need to be able to exclude the cookie from the cache. With Flywheel where my client’s site is hosted, we have to send them a support ticket and they will take care of it.
Step 1
Edit the splash page with Oxygen.
Add a Code Block.
PHP & HTML:

JavaScript:

(function ($) {
'use strict';

function setVisitorCountryCookie(cvalue,exdays) {
// current date and time, specified in the local time zone, as of the time of instantiation.
var d = new Date();

// sets the above date object to when user clicked the button + specified number of days in milliseconds.
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));

// above date converted to a string, using the UTC time zone.
var expires = d.toUTCString();

document.cookie = "actioncoach_visitorcountry=" + cvalue +"; expires=" + expires + ";path=/";
}

// Brasil
$('body').on('click', '#link-501-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "brasil" that should last for 30 days in the browser.
setVisitorCountryCookie('brasil', 30);

window.location.href = '/home-brasil/';

});

// Espana
$('body').on('click', '#link-505-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "espana" that should last for 30 days in the browser.
setVisitorCountryCookie('espana', 30);

window.location.href = '/home-espana/';

});

// Argentina
$('body').on('click', '#link-509-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "argentina" that should last for 30 days in the browser.
setVisitorCountryCookie('argentina', 30);

window.location.href = '/home-argentina/';

});

// Chile
$('body').on('click', '#link-507-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "chile" that should last for 30 days in the browser.
setVisitorCountryCookie('chile', 30);

window.location.href = '/home/';

});

// Colombia
$('body').on('click', '#link-503-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "colombia" that should last for 30 days in the browser.
setVisitorCountryCookie('colombia', 30);

window.location.href = '/home/';
});

// Costa Rica
$('body').on('click', '#link-541-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "costarica" that should last for 30 days in the browser.
setVisitorCountryCookie('costarica', 30);

window.location.href = '/home/';
});

// El Salvador
$('body').on('click', '#link-538-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "elsalvador" that should last for 30 days in the browser.
setVisitorCountryCookie('elsalvador', 30);

window.location.href = '/home/';

});

// Guatemala
$('body').on('click', '#link-535-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "guatemala" that should last for 30 days in the browser.
setVisitorCountryCookie('guatemala', 30);

window.location.href = '/home/';
});

// Mexico
$('body').on('click', '#link-532-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "mexico" that should last for 30 days in the browser.
setVisitorCountryCookie('mexico', 30);

window.location.href = '/home/';
});

// Panama
$('body').on('click', '#link-529-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "panama" that should last for 30 days in the browser.
setVisitorCountryCookie('panama', 30);

window.location.href = '/home/';
});

// Peru
$('body').on('click', '#link-526-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "peru" that should last for 30 days in the browser.
setVisitorCountryCookie('peru', 30);

window.location.href = '/home/';

});

// Republic Dominicana
$('body').on('click', '#link-523-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "republicdominicana" that should last for 30 days in the browser.
setVisitorCountryCookie('republicdominicana', 30);

window.location.href = '/home/';
});

// Ecuador
$('body').on('click', '#link-520-147', function (e) {
e.preventDefault();

// creates a cookie named "actioncoach_visitorcountry" having a value of "ecuador" that should last for 30 days in the browser.
setVisitorCountryCookie('ecuador', 30);

window.location.href = '/home/';
});

}(jQuery));
where link-501-147 is the ID of the Brasil country flag link wrapper, link-505-147 the ID of Espana and so on..
We now have the code in place that will create the actioncoach_visitorcountry cookie and set it to whatever is the country selected by the visitor.
To test this, go to the splash page, open Chrome DevTools, and click on Application tab.
On the left under Storage > Cookies, click on the site name.
Now click on one of the country flags and you should see the corresponding value for the cookie.
Step 2
Install and activate Code Snippets plugin.
Go to Snippets > Add New.
Title: [Condition] Visitor Country
Code:
add_action( 'oxygen_enqueue_scripts', 'wpdd_call_oxygen_vsb_register_condition_visitor_country' );
/**
* Run oxygen_vsb_register_condition().
*/
function wpdd_call_oxygen_vsb_register_condition_visitor_country() {
if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

oxygen_vsb_register_condition(
// Condition Name
'Visitor Country',

// Values: The array of pre-set values the user can choose from.
// Set the custom key's value to true to allow users to input custom values.
array(
'options' => array( 'Brasil or Espana or Argentina', 'Brasil', 'Argentina', 'Espana', 'Chile', 'Colombia', 'Costa Rica', 'El Salvador', 'Guatemala', 'Mexico', 'Panama', 'Peru', 'Republic Dominicana', 'Ecuador' ),
'custom' => false
),

// Operators
array( '==', '!=' ),

// Callback Function: Name of function that will be used to handle the condition
'ac_visitor_visitor_country_callback',

// Condition Category: Default ones are Archive, Author, Other, Post, User
'Other'
);

}

/**
* Callback function to handle the condition.
* @param mixed $value Input value - in this case, the selected choice in the condition dropdown.
* @param string $operator Comparison operator selected by the user.
*
* @return boolean true or false.
*/
function ac_visitor_visitor_country_callback( $value, $operator ) {

if ( '==' === $operator ) {
switch ( $value ) {
case 'Brasil or Espana or Argentina':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && in_array( $_COOKIE['actioncoach_visitorcountry'], array( 'brasil', 'espana', 'argentina' ), true ) );
break;

case 'Brasil':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'brasil' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Espana':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'espana' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Argentina':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'argentina' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Chile':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'chile' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Colombia':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'colombia' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Costa Rica':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'costarica' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'El Salvador':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'elsalvador' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Guatemala':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'guatemala' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Mexico':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'mexico' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Panama':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'panama' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Peru':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'peru' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Republic Dominicana':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'republicdominicana' === $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Ecuador':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'ecuador' === $_COOKIE['actioncoach_visitorcountry'] );
break;

default:
return false;
break;
}
} else {
switch ( $value ) {
case 'Brasil or Espana or Argentina':
return ( ! in_array( $_COOKIE['actioncoach_visitorcountry'], array( 'brasil', 'espana', 'argentina' ), true ) );
break;

case 'Brasil':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'brasil' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Espana':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'espana' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Argentina':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'argentina' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Chile':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'chile' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Colombia':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'colombia' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Costa Rica':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'costarica' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'El Salvador':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'elsalvador' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Guatemala':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'guatemala' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Mexico':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'mexico' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Panama':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'panama' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Peru':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'peru' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Republic Dominicana':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'republicdominicana' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

case 'Ecuador':
return ( isset( $_COOKIE['actioncoach_visitorcountry'] ) && 'ecuador' !== $_COOKIE['actioncoach_visitorcountry'] );
break;

default:
return false;
break;
}
}

}
}
Set the snippet to run everywhere. Save changes and activate.
Step 3
The condition is now ready to be used in any Oxygen Template/Page etc.
In the Conditions dialog, look for Visitor Country under Other.
Reference

Age-Based Access using Cookies and Conditions in Oxygen

Nos experts se tiennent à votre disposition
pour répondre à vos questions et vous conseiller
dans votre projet digital.

Vous souhaitez développer une application ?

Envie d’échanger avec l’un de nos experts ? Besoin d’en savoir plus ?
En savoir plus

Recevez notre newsletter

La protection des données nous tient à cœur. JAG utilise les informations que vous fournissez afin de vous proposer des informations et du contenu pertinent sur nos produits et services. Vous pouvez vous désinscrire de ce type de communications à tout moment. Pour plus d'informations, consultez notre politique de confidentialité
© 2022 JAG Consulting, sas. Tous droits réservés.
© 2022 JAG Consulting, sas. Tous droits réservés.
4.8
Lire les 51 avis
google
Brieuc Pauly
08/09/2022

Créative, dynamique et très professionnelle, c'est à mon sens les piliers qui soutiennent cette équipe talentueuse ! Une agence à taille humaine qui privilégie la qualité et le résultat. Alors si vous souhaitez le meilleur pour votre communication adressez vous les yeux fermés à ces passionnés, plus que compétents ! (Translated by Google) Creative, dynamic and very professional, these are in my opinion the pillars that support this talented team! A human-sized agency that favors quality and results. So if you want the best for your communication, turn your eyes closed to these enthusiasts, who are more than competent!

google
Amaury Tardier
08/09/2022

Merci à Julien et son équipe, plus que satisfait par le travail fourni ! Je recommande vivement ! (Translated by Google) Thanks to Julien and his team, more than satisfied with the work provided! I highly recommend !

Ravi de travailler avec l'équipe JAG pour la gestion de mon site de e-Commerce. Equipe réactive et toujours pleine de bonnes idées pour améliorer les performances du site (Translated by Google) Glad to work with the JAG team for the management of my e-Commerce site. Responsive team always full of good ideas to improve the performance of the site

Merci à Julien pour son accompagnement dans mon projet, je recommande à 200% c est le Top (Translated by Google) Thanks to Julien for his support in my project, I recommend 200% it is the top

Excellente agence. Equipe est très pro, à l'écoute et de bon conseils. (Translated by Google) Excellent agency. The team is very professional, attentive and gives good advice.

Temps de réponse hors pair quand il y a eu un pb sur mon site, je vous recommande chaleureusement Julien :-) (Translated by Google) Unparalleled response time when there was a problem on my site, I warmly recommend Julien :-)

Charger plus
listarrow-right
Julien

Julien

Votre conseiller

I will be back soon

Une question ?

Julien
Bonjour ! Je suis Julien.
Comment puis-je vous aider?
Démarrer la conversation:
chat Live Chat