4

We would like to geolocate visitor IP addresses, and for US-based addresses render a portion of the page to display state-specific options. I actually have the PHP working well to pull the 2-letter state value from a Geocoding API, but am wondering the best way to plug that into Craft PRO.

I'm assuming the logic should look something like this in the end:

  1. Check user for geo-cookie.
  2. If not present, geolocate the user with their 2-letter abbreviation and set the geo-cookie.
  3. If present, read the geo-cookie.
  4. With that value in hand, render page elements accordingly, using the 2-letter abbreviation as a filter. If the user has no 2-letter value (perhaps an international user), present some fall-back "default" content in those areas of the page. (And yes, I am well aware that these services aren't 100% accurate but some providers are fairly close and we can live with that.)

I have found Khalwat's cookie plugin for Craft, which looks like it should suit nicely. But as a newbie to both Craft and Twig, how do I now bring it all together?

OR should we just adapt Craft PRO's localization options for this instead, since it's built in? And if so, how would we pass the 2-letter value along to use that?

4

Let's get the easy question out of the way first... Don't use Craft's localization to handle this task. While the localization feature is amazing and powerful, it doesn't fit the needs you're describing. Localization is mainly geared towards handling different languages for different countries.

So what's the best way to handle this? Fortunately, it sounds like you've already worked out the hard part:

I actually have the PHP working well to pull the 2-letter state value from a Geocoding API

All that's left now is to integrate that business logic into your Craft site! There are a few different ways to approach this, but I'll lay it out in a way that's simple, straightforward, and best fits your current scenario...

  1. Grab and install the Business Logic plugin (unless you've already got a custom plugin installed, in which case you can just build on that).

  2. Create a new function in the Variable file. Let's name it something like detectState.

  3. Put all of your existing PHP logic into that function. You can even get, set, and check your cookies straight from this function. Using a separate cookie plugin may not even be necessary in this situation, since it's easy to manage cookies directly from PHP.

  4. Have your function return the 2-letter state code.

  5. In Twig, you'd call your new function similarly to this:

    {% set stateCode = craft.businessLogic.detectState %}
    

Voila! You can now use stateCode to handle whatever Twig switching logic you need.

| improve this answer | |
  • Nice answer Lindsey. Seems we were following the same script, starting with the ever faithful BusinessLogic plugin. You definitely beat me to the punch — saw that 'entry changed' line pop-up ;) – Douglas McDonald Jun 10 '15 at 17:27
  • Lol, only by a few seconds. I saw yours too, and thought "jinx"! :) – Lindsey D Jun 10 '15 at 17:28
  • Thanks to both of you - I am already cooking with gas. I mainly wasn't familiar with how to inject normal PHP into Twig/Craft, so both of your posts were really helpful. Thx. – Neal Magee Jun 11 '15 at 12:18
  • Btw, if you're curious -- the GeoIPs IP geolocation service is a good quality API to use for this. There's a free level, and then tiers above that, but they give fast, accurate results and are easy to work with on a code/authentication level. Another 20 lines of PHP and this was done. – Neal Magee Jun 12 '15 at 14:28
3

I think that you've outlined the process perfectly. To put the pieces together I would suggest that you create a small plugin (perhaps using the trusty BusinessLogic plugin as a starting point). To communicate with your plugin, add a 'template variable' method to your plugin's variables file, and your geolocation php. For more info see the docs on template variables.

When your template first renders, check for your geo-cookie and assign it to a variable. If the cookie doesn't exist then perform the 'geolocation' using your plugin's template variable method (i.e. 'geoCode'), and set the cookie (i.e. 'myGeoCookie').

{% if getCookie('myGeoCookie') %}
    {% set geoCode = getCookie('myGeoCookie') %}
{% else %}
    {% set geoCode = craft.businessLogic.geoCode %}
    {% 'myGeoCookie'|setCookie(geoCode, now | date_modify("+120 days").timestamp ) %}
{% endif %}

In your plugin's template variable 'geoCode' method you can either perform your geolocation lookup directly and return the resulting 2 letter code, or use it as a wrapper to call a corresponding 'service' method which then passes back the result to the variable, which then returns it to the template.

public function geoCode()
{
    // perform lookup
    return geoCode;
}

Now with the 'geoCode' variable in your template, you can use it to lookup and display the state specific content from your 'states' section in craft (i.e. called 'stateContent' or whatever you want to call it), which also has a corresponding 'geoCode' field or other identifying attribute (i.e. title). Otherwise display your default content.

{% if geocode %}
    {% set stateEntry = craft.entries.section('stateContent').geoCode(geoCode).first() %}
    {{ stateEntry.body }}
{% else %}
    {% otherwise display default content %}
{% endif %}

Not tested. But should be a good starting point.

| improve this answer | |
0

I'm very glad to have written this myself -- it's working well, and I plugged into the GeoIPs (paid) service for reverse-IP geolocation. http://www.geoips.com/en/

But I also found this great plugin that seems to do the trick right out of the box, though it is not cookie-based. https://github.com/lukeholder/craft-geo/

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.