How to get a better Google snippet description

I suspect you’ve always wondered how google gets the snippet information that it displays with the link to your site in it’s SERPs. Well I have too, and now i’ll let you in on the secret http://googlewebmastercentral.blogspot.com/2007/09/improve -snippets-with-meta-description.html

“We want snippets to accurately represent the web result. We frequently prefer to display meta descriptions of pages (when available) because it gives users a clear idea of the URL’s content.”

So it seems that google does care about meta descriptions after all…

How do we apply this to dynamically generated products then I hear you ask. Well firstly I’m going to imagine that you have your product information stored in a database or flat file format and you know your SQL/parsing queries that will pull the relevant tags.

Using the following SQL (made for MySQL) we can generate information for our meta description quite easily.

SQL to create the tables


CREATE TABLE manufacturers (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
) Engine=InnoDB;


CREATE TABLE categories (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);


CREATE TABLE products(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price NUMERIC NOT NULL,
quantity INT UNSIGNED NOT NULL,
short_description VARCHAR(255),
long_description TEXT,
manufacturer_id INT UNSIGNED NOT NULL,
category_id INT UNSIGNED NOT NULL,
CONSTRAINT FOREIGN KEY (`manufacturer_id`) REFERENCES manufacturers(`id`)
CONSTRAINT FOREIGN KEY (`category_id`) REFERENCES categories(`id`)
) Engine=InnoDB;

The format google expects

From the google blog we can see that the general format is:

Field: Value, [Value, ...] [N]

e.g. <meta name=”Description” content=”Manufacturer: Samsung, Category: Electronics, TVs, Features: HDTV Ready, Resolution: 1680×1050, Contrast: 1000:1, Size: 19 inches, Price: $17.99, Quantity: 5″ />

SQL to retrieve meta description information

So to generate our lovely meta description we can use the following code:

SELECT p.name as 'product'
, CONCAT('Manufacturer: ',m.name) as 'manufacturer'
, CONCAT('Category: ',GROUP_CONCAT(c.name ORDER BY c.name ASC SEPARATOR ', '),',') as 'category'
, CONCAT('Price: ',p.price) as 'price'
, CONCAT('Quantity: ',p.quantity) as 'quantity'
, CONCAT((
SELECT GROUP_CONCAT(CONCAT(pf.`key`,': ',pf.`value`) SEPARATOR ', ')
FROM product_features pf
WHERE pf.product_id = p.id
),',') as 'features'
, CONCAT('Description: ',p.short_description,'') as 'description'
FROM products p
JOIN manufacturers m ON p.manufacturer_id = m.id
JOIN product_categories pc ON pc.product_id = p.id
JOIN categories c ON c.id = pc.category_id
WHERE p.id = 1
GROUP BY m.name, p.price, p.quantity

PHP to create the html content


<?php
// Other code...
// Assuming connection database/connection has been performed already above
$sql = <sql listed above>;
$res = mysql_query($sql);
while($row = mysql_fetch_assoc($res)){
$metaSet[] = $row;
}
?>
<html>
<head>
<title><?php echo $metaSet['product'];?></title>
<meta name="Description" content="<?php echo $metaSet['manufacturer'].' ', $metaSet['category'].' ', $metaSet['price'].' ', $metaSet['quantity'].' ', $metaSet['features'].' ', $metaSet['description'];?>" />
</head>
<body>
.../// stuff here
</body>
</html>

Notes

It is of course worth noting that we have selected a particular product (i.e. by id) in this example. Also that the meta title should include the name of the product, e.g. <title>Samsung Pebble SM932MW 19″ HD Ready Widescreen Monitor</title>

Because we’re only expecting 1 row to be returned (as product_id is the PRIMARY KEY) we could use PHP’s extract() function on the $row instead of putting all the information into a dataset. Allowing us to reference $manufacturer instead of $metaSet['manufacturer'], however I have my own reservations about implementing this sort of “functionality” in case of variable name clashes. Although it is possible to use the EXTR_PREFIX_ALL flag specified with a prefix string to avoid the situation of overwriting previous variables.

Limitations

Limitations of the above SQL: group_concat only takes up to 1024 characters

Tags: , ,

Leave a Reply