Google Trends is an amazing tool which allows you to understand search interest in a specific topic over time. The ability to monitor these topics in specific regions is really useful for international content campaigns. It ultimately allows you to create engaging content by staying ahead of the curve and understand new trends as soon as they emerge.
Sadly, there is no official Google Trends API, we look at getting the trend data from an alternative tool. From there, we can discuss some of the options available and some of the transformations we ourselves can make to the raw data we have collected and use it in a web application or research project.
Briefly, it is useful to give a quick tech overview for the task at hand. The tool we will use to scrape the data utilises PHP and we also need NPM and we will use Packagist to install any packages we need. As we are using a server-side language, a virtual server would be required such as WAMP or MAMP. In terms of optional apps, it may be useful to have a database tool to store the raw data that is collected.
Introduction to a Google Trends Alternative
As mentioned earlier, Google Trends does not currently have an official API. So if you want to make a web application with its data, an unofficial tool will be required. For this task, we will use G-Trends by X-Fran, a great free tool that provides us with more than enough to get the data we need.
We will look at some of the features offered by G-Trends that mirror the functions of the official Google Trends application. For example, G-Trends provides options to receive data for Related Queries and Suggestions Autocomplete – both of which are great for keyword inspiration in your relevant industry or topic.
We will be making use of the Interest Over Time feature of Google Trends because we will be looking retrospectively at the trend data for a specific keyword, in a specific country, over the course of six years. With the historical data, we can make a few assessments and spot seasonality and other trends, which give us an insight into future opportunities.
Making the Interest Over Time G-Trends Request
Create a new project in the development environment of your choice and open the command line. Enter the following command to install the packages used by G-Trends:
Note – There can sometimes be SSL issues with local virtual servers. Please refer to the WAMP or MAMP guides on creating locally issues SSL certificates for your computer. Additionally, there may be interference with anti-virus apps blocking the download. To get round this, please refer to guides on your specific anti-virus software and how to allow WAMP / MAMP to be exempt from the security checks.
Running the command above with create the files for all the packages required as well as some composer configuration files. Inside this folder, we can make a subfolder and it can be called as appropriate – I will call it “example”. It is best to keep your working files separate from the folder with PHP packages for organisation and security reasons. Inside this folder, make an index.php file and add the line to include vendor’s autoload file. Then we include the GTrends class from the namespace to include GTrends in our project.
Once installed, the GitHub repository gives a nice example of setting up your options and deciding what data you want to collect. The example covers several of the controls seen on Google Trends, such as language, country, timeframe and time zone, as shown below.
Now, we will look at some of the specific options selected and a brief reason why they were chosen.
- Country Code (Geo) – The first option to set is the country code. This is available in Google’s documentation or you can use Google Trends and grab the codes from the URL. For example, the United Kingdom would be selected with GB.
- Language (HL) – Next is to select the language within the country. The language code is available in Google’s documentation. For example, British English would be selected as en-GB.
- Timeframe (Time) – Next, is to set the date range to a suitable period of time. Currently, it needs to be at least 6 years long in order to get the data split by each month. The range can be set by adding two dates in YYYY-MM-DD format e.g. 2019-05-01 2025-05-31.
- Search Topic (Category) – I keep the category set to 0 so any trends data collected is as broad as possible.
- Time Zone (TZ) – I also keep the time zone to 0 so it can default to the suitable time zone of the selected country. Also, because the timeline is over several years, the time zone does not come into play.
Once the options are set up, we create a new G-Trends request by creating a variable and passing the options as a parameter to the G-Trends function. Looking at the documentation, the G-Trends function we are using is getInterestOverTime. We pass the keyword as a parameter to tie the keyword to a country at a specific date range.
Processing the Raw Trends Data
Initial Review and Language Considerations
Now we have made the request, we want to do some basic formatting so we can review the data. First, we can use HTML <pre> tags to ensure the text output maintains the same indents, line breaks and spaces so we can read it more easily. We can use the json_encode() function to convert the PHP array into JSON format. Finally, JSON_PRETTY_PRINT adds the line breaks and indentation that the <pre> tags are preserving.
It is at this point, you could store the JSON data and then switch to using a language of your choice, if PHP is not your first pick. The new format is universally compatible with other languages, including front-end languages such as JavaScript. The way the data is processed depends on which development library is being used. The data can be processed using standard PHP or a PHP library such as CodeIgnitor. The data could be processed stored in a database using a package such as Medoo. For our example, we will use standard PHP for the sake of clarity.
Process the Raw Data With PHP
Using PHP, the data coming back from GTrends is already a PHP array so we can start working with it right away. An array is a common method of processing API requests and carrying out some manipulations later on. Next is to set a variable for the months data by looking inside the array we just created and reference the array keys of TIMESERIES, then data and finally timelineData. This way, we can focus out attention specifically on the part of the array which has the month scores we need.
To finish off the preparation, we can create an empty Trend Scores variable to store the score values in an array. Using the echo feature of PHP, we can create a heading for the section we will create on the page featuring the list of months and trend scores. Now the data has been reviewed, we can also comment out the <pre> tags we made earlier, as they are no longer required.
Now we use the variable storing the trend array data and cycle through each individual month using a foreach loop. Inside the loop, the date of the month can be accessed by using “time” array key. To process the raw data for your application, it would be a good idea to turn the date into a suitable format by using the date function. In this case, “Y-m-d” provides the date in a YYYY-MM-DD format.
Similarly, the trend score for the month is accessible with the “value” array key. Adding both the month name and the suitable score to a pair of variables enables us to use the values again later in the application. For instance, we can use echo to display the values on the screen. Appending a line break after each value will make the content more manageable. Also, putting a <hr> before the end of the loop will add a horizontal row under each month and score combination.
Conclusion
Whilst we do not have an official Google Trends API, G-Trends by X-Fran is a great tool that gets you more than enough trends data for your application. The ability to conduct ongoing monitoring of various trends lets you stay ahead of the curve and make smart, informed business decisions.