Display specific Drupal contents based on your visitor's country location in Views (using Smart IP and Location modules) and display all contents if there's no result

This tutorial will show how to setup a View that shows list of contents in a Drupal 7 site that are filtered based on your visitor's country location with the use of Smart IP and Location contributed modules and if there is no result, the fall back is to display all contents. Smart IP module is used to identify Drupal site visitor's geographical location based on his IP address and Location module is used to provide the field and expose views location filters.

Steps:

The following steps will list all the nodes with country location field stored content same as the value of my country detected by Smart IP.

  1. Install Smart IP, Smart IP Views Bridge, Location, Location CCK, Ctools, Views and Views UI modules. For debug purposes, enable Device Geolocation module.

  2. Setup a content type or use an existing content type (in this example we will use the existing "Basic page" content type) and add a new field with "Location" field type at http://www.yoursite.com/?q=admin/structure/types/manage/page/fields. Populate the field label with "Branch". Select "Location" from Type of data to store and click "Save" button.

    Add location field

  3. At "Location" field settings > Locative Information > Collection Settings select "Allow" for the "City" and "Country" items and click "Save field settings" button.

    Location field collection settings

    Use the default values for succeeding setup settings of this field.

  4. Create some "Basic page" content. Populate the "Title", "City" fields and select a country.

    Create Basic page

    Note: Make sure that at one of your created node's country location field value matches your country geolocation that Smart IP has detected based on your IP. To check, enable the Device Geolocation's block at http://www.yoursite.com/?q=admin/structure/block.

  5. Setup a new Views that will display the nodes under the content type with Location field you have setup earlier.

    Create views page

  6. There are two ways to add filter based on Smart IP detected country location: the normal filter and the contextual filter:

    1. First way:

      Add a filter criteria using "Location: Country" filter. Make sure to select "This page (override)" from "For" dropdown box at upper left.

      Add views Location: Country filter

      Under the "Operator" select "Is", select "Smart IP: visitor's country code" item from "Country" dropdown box and click "Apply" button.

      Configure views Location: Country filter

    2. Second way:

      Add a contextual filter using "Location: Country" filter. Make sure to select "This page (override)" from "For" dropdown box at upper left.

      Add views Location: Country contextual filter

      Select "Use Smart IP detected visitor's country value" item from "WHEN THE FILTER VALUE IS NOT IN THE URL" radio buttons and click "Apply" button.

      Configure views Location: Country contextual filter

  7. To display all the "Basic page" nodes if there's no result, we will use the "No results behavior" and a view display that list all the "Basic page" nodes. Add a views attachment and save your views.

    Add attachment views display

  8. Go to the "Page" view display. Add a "No results behavior" and select "Global: View area". Make sure to select "This page (override)" from "For" dropdown box at upper left.

    Add a "No result behavior"

  9. Select "View: drupal_branches - Display: attachment_1" item from "View to insert" dropdown box.

    Configure "Global: View area" no result behavior

    Note: if you did not see the "View: drupal_branches - Display: attachment_1" item, save your views and try again.

  10. Save your views.

  11. Go to the page generated by the views you have created and you should see all the nodes with country location field stored content same as the value of your country detected by Smart IP.

    Views filter country result page

    If there's no "Basic page" nodes to display for the Smart IP detected country, it should display all the "Basic page" nodes as fall back.

    Display all contents on no result

Comments

The ad module uses Ad GeoIP module for geotargeting, but it not available for D7. Can I use smart IP module for that purpose?

The purpose is to enable geo-targeting feature for ad module. Geo-targeting enables you to show relevant ads to visitors depending on their country.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.