Using Embed Code? Randomize your Links & Anchor Text

Learn how to not get penalized for your infographics or widgets by using the same links and anchor text in your embed code over and over.

Using Embed Code? Randomize Your Links & Anchor Text

**UPDATE** Australia’s own Tom Johns was awesome enough to turn this into a WordPress Plugin.

One of the recurring questions I get in the office when we’re discussing the launch of widgets and infographics is “how aggressive should we be with the anchor text in the embed code?” This is likely the result of Matt Cutts’ threat on infographics last year and legends of widgets that led to penalties (raise your hand if you knew the Oatmeal used to work at SEOmoz).

With most SEO conundrums I think about them as a developer before I think about them as a marketer. Google’s algorithms and updates look to identify patterns, so the best way to future-proof your link building efforts is not to create any negative patterns. This is true not just of infographics, but also of guest posts and any other link building tactic that’s being run into the ground in the name of “scale.”

When people ask me about the aggressiveness of the anchor text in the embed code, I usually say go with a branded term or you can randomize the anchor text. Here’s how you can do that in PHP.

[code type=php]
function randomAnchor()
{

$anchors = array(‘keyword 1’, ‘keyword 2’, ‘keyword 3’, ‘keyword 4’, ‘keyword 5’);
return $anchors[0,rand(count($anchors)-1)];

}

[/code]
To even a beginner developer the idea is quite simple. Essentially you create a function that takes a list of keywords that you wish to use as anchor text and it picks randomly from the list. The next step is making that function run in the spot where your embed code is rendered in the HTML, like so:

[code type=html]
<textarea>

<img src=”http://www.example.com/image.jpg” alt=”example alt text”>
<a href=”http://www.example.com”><?=echo randomAnchor();?></a>

</textarea>

[/code]

Now what happens is whenever your site loads the embed code randomly pick from the 5 keywords and place it. You can do this for any number of keywords just add,’keyword’to the array like so:

[code type=php]

$anchors = array(‘keyword 1’, ‘keyword 2’, ‘keyword 3’, ‘keyword 4’, ‘keyword 5’, ‘keyword 6’, ‘keyword 7’, ‘keyword 8’, ‘keyword 9’, ‘keyword 10’, ‘keyword 11’, ‘keyword 12’, ‘keyword 13’, ‘keyword 14’, ‘keyword 16’);

[/code]

It will continue to work no matter how many keywords you add to it because the second line of code counts the number of members in the array before it randomly picks from it.

JavaScript Version

Realistically if you’re working on a client’s site it may be a hard sell to get them to install some server side code just to randomize your anchor text. Also a lot of sites are not using PHP and while I could rattle this function off in a variety of different languages, the next best thing would be to do it with JavaScript. To keep this simple and avoid the need to do some sort of find and replace function we’re just going to randomize the whole link rather than just the anchor text and then we’re going to inject it at the bottom of the textarea named “EmbedText”.

[code type=javascript]

<script type=”text/javascript”>

function randomLink()
{

var links = new Array();

links[0] = “<a href=”http://www.example.com/link1″>anchor 1</a>”;
links[1] = “<a href=”http://www.example.com/link1″>anchor 2</a>”;
links[2] = “<a href=”http://www.example.com/link1″>anchor 3</a>”;
links[3] = “<a href=”http://www.example.com/link1″>anchor 4</a>”;
links[4] = “<a href=”http://www.example.com/link1″>anchor 5</a>”;
links[5] = “<a href=”http://www.example.com/link1″>anchor 6</a>”;

EmbedText.value += links[Math.floor(Math.random()*(links.length-1))];

}
</script>
[/code]

Just as with the PHP version you can add as many links as you’d like. Subsequent links would be added by increasing the number in the array index (the number in the brackets) like so:

[code type=javascript]

links[6] = “<a href=”http://www.example.com/link1″>anchor 6</a>”;
links[7] = “<a href=”http://www.example.com/link1″>anchor 7</a>”;
links[8] = “<a href=”http://www.example.com/link1″>anchor 8</a>”;

[/code]

Just be aware that you need to escape the quotations when declaring the links in the array which means you have place a slash () in front of the quotes (“).

When you place your embed code make sure the <textarea> has id=”EmbedText” otherwise it will not work. After you place your embed code on your site place the following code below it.

[code type=javascript]

<script type=”text/javascript”>
randomLink();
</script>

[/code]

As as an SEO I would prefer you place the first code snippet into a file and link to it from the <head> section of the page with the <script type=”text/javascript” src=”filename.js”></script> to help with the page speed (removing inline JS and allowing the browser to cache the code). If you don’t have that option you can place it directly into the <head> tag.

However as this is about avoiding the creation of patterns those of you that are Tinfoil Hat SEOs can feel free to change the names of the functions and the ID on the textarea, but then again if you’re that paranoid you would likely avoid the JavaScript method altogether.

Here’s that code at work (hit reload to see it randomize):
[cc lang=”javascript”]

// <![CDATA[
function randomLink()
{
var links = new Array();
links[0] = “anchor 1“;
links[1] = “anchor 2“;
links[2] = “anchor 3“;
links[3] = “anchor 4“;
links[4] = “anchor 5“;
links[5] = “anchor 6“;
EmbedText.value += links[Math.floor(Math.random()*(links.length-1))];
}
randomLink();
// ]]>
[/cc]

A note on the Paddy Moogan Method

I am a huge fan of the Inception-esque embed code within an embed code method popularized by the magnanimous Paddy Moogan. However if you’re going to use that, I would err on the side of caution and only use branded anchors and links. The concern being that it is only the JavaScript version is not backend-specific and embedding the JavaScript version an easy pattern to detect. In other words if you were to use the PHP version it would only work for users whose sites are using PHP and that’s virtually impossible to determine therefore you will have to use JavaScript.

Bonus Round

Here’s two more PHP functions.

First, the PHP version of randomLink:

[code type=php]
function randomLink()
{

$links = array(‘<a href=”http://www.example.com/link1″>anchor 1</a>’, ‘<a href=”http://www.example.com/link2″>anchor 2</a>’, ‘<a href=”http://www.example.com/link3″>anchor 3</a>’, ‘<a href=”http://www.example.com/link4″>anchor 4</a>’, ‘<a href=”http://www.example.com/link5″>anchor 5</a>’);

return $links[rand(0,count($links)-1)];

}

[/code]

Second, here’s a function that randomizes the link and the anchor text. I would only use this for branded anchor text.

[code type=php]
function randomLinkAndAnchor()
{

$urls = array(‘http://www.example.com/link1’, ‘http://www.example.com/link2’, ‘http://www.example.com/link3’, ‘http://www.example.com/link4’, ‘http://www.example.com/link5’);

$anchors = array(‘keyword 1’, ‘keyword 2’, ‘keyword 3’, ‘keyword 4’, ‘keyword 5’);
$urlIndex = rand(0,count($urls)-1);

$anchorsIndex = rand(0,count($anchors)-1);
return ‘<a href=”‘.$urls[$urlIndex].'”>’.$anchors[$anchorsIndex].'</a>’;

}

[/code]

Have fun!

TIPS, ADVICE, AND EXCLUSIVEINSIGHT DIRECT TO YOUR INBOX

We unpack industry news, updates and best-practicesin our newsletter