Get Your Google Search Position in C#

    February 2, 2007

If you want to test your position in Google for a certain search term, you can do so by using the Google website. By position I don’t mean Page Rank, but the actual place in the search results.

You can also use C# to find the position like the method shows below.

You can use the code to build a cool application that lists the positions of your website based on all your important search terms. You can change it to run asynchronously for better performance when checking multiple search terms.

/// <summary>
/// Retrives the position of the url from a search
/// on using the specified search term.
/// </summary>
public static int GetPosition(Uri url, string searchTerm)
string raw = "{0}&btnG=Search";
string search = string.Format(raw, HttpUtility.UrlEncode(searchTerm));

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
string html = reader.ReadToEnd();
return FindPosition(html, url);

/// <summary>
/// Examins the search result and retrieves the position.
/// </summary>
private static int FindPosition(string html, Uri url)
string lookup = "(<a class=l href=\")(\\w+[a-zA-Z0-9.-?=/]*)";
MatchCollection matches
= Regex.Matches(html, lookup);

for (int i = 0; i < matches.Count; i++)
string match = matches[i].Groups[2].Value;
if (match.Contains(url.Host))
return i + 1;

return 0;

Examples of use

You simply provide the method with your website URL and the search term to test and it returns the position.

Uri url = new Uri("");
int position = GetPosition(url, "search term");

Google+Search+Position+In+C.aspx” class=”bluelink”>Comments

Add to | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.