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

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

Striving to adjust the operation of our APIs to fit your needs better, in version three we have introduced several options for rank tracking with SERP API:

1 A target parameter available when setting Live Google Organic SERP API tasks;
2 A ready-made script that can be applied to a wide range of search engines.

Let’s explore how to leverage both rank tracking solutions in SERP API v3.

Using the target field for rank tracking

The target parameter is exclusively available in Regular and Advanced endpoints of Live Google Organic SERP API.

To get started with this option, simply specify a domain, subdomain or webpage in the target field when setting a task, and our API will return results only for that target. It’s important to note that results of target-specific tasks will only include SERP elements containing a url string in their structure.

When indicating a target, you can utilize the wildcard character (‘*’) to define search patterns and narrow down results. Here are some examples:

example.com – return results for the website’s home page with URLs https://example.com, or https://www.example.com/, or https://example.com/;
*example.com – returns results for the website’s home page regardless of the subdomain, such as https://en.example.com/;
example.com* – returns results for the domain, including all its pages;
*example.com* – returns results for the entire domain, including all its pages and subdomains;
example.com/example-page – returns results for the exact URL;
example.com/example-page* – returns results for all domain’s URLs that start with the specified path.

The body of your POST request to Live Google Organic SERP API endpoints (Live or Advanced) should be structured like this:

[
   {
       "language_name": "English",
       "location_name": "United States",
       "keyword": "albert einstein",
       "target": "*wikipedia.org*"
   }
]

Learn more about the target parameter in our documentation:

Live Google Organic SERP API Regular
Live Google Organic SERP API Advanced

Collecting search rankings with a PHP or Python script

The second option to collect SERP data for a specific target can be applied to a variety of search engines and functions. It’s based on retrieving a complete overview of the necessary SERP elements 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.:

  • /v3/serp/google/organic/task_get/regular/ – if you need to retrieve the regular results
  • /v3/serp/google/organic/task_get/advanced/ – for advanced SERP results
  • /v3/serp/google/maps/task_get/advanced/ – if you have set Google Maps SERP task
  • /v3/serp/bing/local_pack/task_get/regular/ – if you have set Bing Local Pack SERP task

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:

i

f (($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

In conclusion, with the target parameter and the option to use a ready-made script, you now have versatile tools at your disposal to tailor rank tracking with SERP API to your specific needs. We hope these features enable you to make well-informed decisions and enhance your workflow.

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

Irene T.

Content Writer at DataForSEO. Irene’s linguistic background and hands-on customer support experience help her brilliantly interpret the most intricate tech topics for any audience. Together with the DataForSEO Content Team, Irene also conducts in-depth researches of the martech industry. Sign up for our newsletter to never miss the latest pieces.

No Comments

Sorry, the comment form is closed at this time.

Embed DataForSeo widget on your website


Embed code:
Preview: