Site icon DataForSEO

Rank Tracking in SERP API v3: New Perspective [Useful Scripts Attached]

Within the framework of SEO research, ranking data from SERPs cannot be understated. Here at DataForSEO, we understand what value it represents to our clients full well.

Striving to adjust the operation of our APIs to fit your needs better, in version three we have incorporated the rank tracking feature into SERP API to provide you with ranking data on more types of SERP elements than the Rank Tracker API in v2 could. Although this solution seemed logical and convenient at first sight, this, in fact, turned out wrong.

So, the substantial change we have recently introduced is eliminating the “target” field and “target_rankings” array in SERP API v3. However, we have worked out a more effective and accurate solution for you. We’ll explore what has changed, why and how to leverage SERP API v3 to broaden the scope of website ranking data.

What’s changed

First of all, taking into account the valuable feedback from our clients, we realized that the “target_rankings” array failed to provide comprehensive information about the discovered SERP elements. Using this option, you could get only the following data about each search result found for the defined domain:

type – type of element
rank_group – the position of the target domain or URL in the group of elements of one type
rank_absolute – absolute rank in SERP for the target domain or URL
url – the URL of the SERP element linked to the target domain or URL

Another downside was that the “target_rankings” array compiled all types of elements from SERP related to the domain. This meant that when you needed to get data on a particular type of element, e.g. “paid”, you still had to filter out the array. Considering all this, we decided to change the approach.

Diving into the new approach

Now you can retrieve a complete overview of the necessary SERP element related to the required domain with the help of the script provided below.

<?php
// You can download this file from here https://cdn.dataforseo.com/v3/examples/php/php_RestClient.zip
require('RestClient.php');
//you can use the '$id' string as a $id variable and '$tag' as $tag variable. we will set the necessary values before sending the request.
//http://your-server.com/pingscript?id=$id
if ((isset($_GET["id"])) and (!empty($_GET["id"]))) {
	// Instead of 'login' and 'password' use your credentials from https://app.dataforseo.com/api-dashboard
	$client = new RestClient('https://api.dataforseo.com/', null, 'login', 'password');
	try {
		$result = array();
		$api_result = $client->get('/v3/serp/google/organic/task_get/regular/' . $_GET["id"]);
		if (((int)$api_result['status_code'] > 0) and ((int)$api_result['status_code'] < 30000)) { //see https://docs.dataforseo.com/v3/appendix/errors/
			foreach($api_result['tasks'] as $task) {
				if (((int)$task['status_code'] > 0) and ((int)$task['status_code'] < 30000)) { //see https://docs.dataforseo.com/v3/appendix/errors/
					//we set 'site' value when setting the task
					if ((isset($task['data']['tag'])) and (!empty($task['data']['tag']))) {
						$site = $task['data']['tag'];
						foreach($task['result']['items'] as $serp) {
							//looking only for organics
							if (($serp['type'] == 'organic') and (parse_url($serp['url'], PHP_URL_HOST) == $site)) {
								$result[] = $serp;
							}
						}
					}
				} else {
					http_response_code(204);
				}
			}
		} else {
			http_response_code(204);
		}
		print_r($result);
		// do something with result
	} catch (RestClientException $e) {
		echo "\n";
		print "HTTP code: {$e->getHttpCode()}\n";
		print "Error code: {$e->getCode()}\n";
		print "Message: {$e->getMessage()}\n";
		print  $e->getTraceAsString();
		echo "\n";
	}
	$client = null;
} else {
	http_response_code(204);
}
?>

When you will be setting a task, you should add the “tag” and “pingback_url” fields to the body of the POST request. The first field should be used for defining the domain, URL, or web page you need data on, while in the second one you should provide the URL path to our script stored on your server, e.g.:

http://your-server.com/pingscript?id=$id
http://your-server.com/pingscript?id=$id&tag=$tag

Your POST request to SERP API should be structured like this:

POST https://api.dataforseo.com/v3/serp/google/organic/task_post 

[
   {
       "language_name": "English",
       "location_name": "United States",
       "keyword": "albert einstein",
       "tag": "wikipedia.org",
       "pingback_url": "https://your-server.com/pingback.php?id=$id"
   }
]

In this way, once the task is completed, you will receive the pingback.php file with the required results to your server.

Now let’s get back to understanding the script and adjusting it to fit your needs. To begin with, you need to download the RestClient.php for running it. The login and password here should be replaced with your credentials from the account dashboard.

$client = new RestClient('https://api.dataforseo.com/', null, 'login', 'password');

In the second line of the try block, you should specify the type of SERP that will be retrieved without appending the id at the end of it, e.g.:

The following line of code represents the arrays that will be cycled through to filter out the necessary elements:

foreach($task['result']['items'] as $serp)

Finally, this if statement contains the condition where you should define what type of SERP element will be retrieved for the specified domain:

if (($serp['type'] == 'organic') and (parse_url($serp['url'], PHP_URL_HOST) == $site))

For example, you can change it to:

if (($serp['type'] == 'paid')...
if (($serp['type'] == 'local_pack')...
if (($serp['type'] == 'featured_snippet')...

If you set multiple tasks for several domains in a POST request, remember to specify the domain in “tag” and add the “pingback_url” for each of them. After the script collects the results, you will find them as pingback.php files by the path defined as “pingback_url”.

Wrap Up

Simply put, the “target” field and “target_rankings” array in SERP API v3, unfortunately, did not provide as much value as expected, and therefore were eliminated. Instead, we have prepared a more convenient solution for you. Retrieving rankings for a domain using this script allows getting more specific and also provides a broader scope of data.

Below you can download the rank tracking script for PHP or Python.
DOWNLOAD FOR PHP DOWNLOAD FOR PYTHON

Exit mobile version