Skip to content

ip2country_name

ip2country_name(ip)

Description

Get country_name of ip

This functions uses IP address data powered by IPinfo and released under Creative Commons Attribution-ShareAlike 4.0 International License. You are required to attribute IPinfo to use their free datasets. The attribution requirements can be met by giving their service credit as your data source. Simply place a link to IPinfo on the website, application, or social media account that uses their data.

Usage

Call or Deploy ip2country_name ?
Call ip2country_name directly

The easiest way to use bigfunctions

  • ip2country_name function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy ip2country_name in your project

Why deploy?

  • You may prefer to deploy ip2country_name in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

ip2country_name function can be deployed with:

pip install bigfunctions
bigfun get ip2country_name
bigfun deploy ip2country_name

Requirements

ip2country_name uses the following secrets. Get them by reading the documentation link and store them in Google Secret Manager in the project where you deploy the function (and give Accessor role to the service account of the function):

name description documentation to get the secret
ipinfo_token IP Info Token doc

Examples

select bigfunctions.eu.ip2country_name("152.216.7.110")
select bigfunctions.us.ip2country_name("152.216.7.110")
select bigfunctions.europe_west1.ip2country_name("152.216.7.110")
+---------------+
| country_name  |
+---------------+
| United States |
+---------------+

Use cases

A use case for the ip2country_name function would be analyzing website traffic logs to understand the geographical distribution of visitors.

Scenario: You have a table in BigQuery containing website access logs, including the IP address of each visitor. You want to determine the number of visitors from each country.

Implementation:

SELECT
    bigfunctions.<your-region>.ip2country_name(client_ip) AS country,
    COUNT(*) AS visitor_count
FROM
    `your-project.your-dataset.website_logs`
GROUP BY
    country
ORDER BY
    visitor_count DESC;

Replace <your-region> with the appropriate BigQuery region for your data (e.g., us, eu, us-central1). Replace your-project.your-dataset.website_logs with the actual path to your website logs table. The client_ip field should contain the IP address of the visitor.

Result: This query will output a table showing the number of visitors from each country, ordered from highest to lowest. This information can be used for various purposes, such as:

  • Targeted marketing: Tailoring marketing campaigns based on the countries with the most visitors.
  • Content localization: Prioritizing translation of website content into the languages spoken in the countries with the highest visitor numbers.
  • Performance optimization: Identifying countries with slow access times and investigating potential network issues.
  • Security analysis: Detecting suspicious activity from specific countries or regions.

This is just one example. The ip2country_name function can be useful in any scenario where you need to determine the country associated with an IP address, such as analyzing server logs, security event data, or e-commerce transactions.


Need help or Found a bug?
Get help using ip2country_name

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about ip2country_name

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on