Alexa Graph and Badge Shortcodes for WordPress

Alexa is a web analytics company of the Amazon brand. The website statistics published by Alexa are extrapolations from data gathered from browsing habits and other sources. These stats can be used to compare and evaluate websites or – more frequently – to show off a website’s global and local rank. The official widget scripts for displaying rank data are found here.

Alexa Internet Screenshot Courtesy of Wikepedia
Alexa Internet Screenshot Courtesy of Wikepedia

The official scripts can be displayed in widget areas on WordPress blogs but the scripts break when embedded into WordPress pages and posts.

We can craft a shortcode to allow us to place Alexa traffic scripts into WP posts and pages. The snippets below create those shortcodes.

The outbound links in the below snippets have the rel=”nofollow” attribute to protect your SEO and the target=”_blank” attribute to make the links open in new tabs.

Need help to add these snippets? Instructions here.

Alexa site rank stats button shortcode

This code displays a rank banner.

The shortcode

[alexa-rank domain=”example.com” url=”http://example.com”]

The code

/* Add Alexa Site Stats Button Shortcode */
function alexa_rank_shortcode( $atts ) {

    // Attributes
    extract( shortcode_atts(
        array(
            'domain' => '',
            'url' => '',
        ), $atts )
    );

    // Code
    return '<p><a rel="nofollow" target="_blank" href="http://www.alexa.com/siteinfo/'.esc_html($domain).'"><script type="text/javascript" src="http://xslt.alexa.com/site_stats/js/s/c?url='.esc_html($url).'"></script></a></p>';
}
add_shortcode( 'alexa-rank', 'alexa_rank_shortcode' );

Alexa traffic graph shortcode

This shortcode displays a site traffic comparison graph.

The shortcode

[alexa-graph first=”example.com” second=”example.com” third=”example.com”]

Specify 1, 2 or 3 domains to display their traffic. The shortcode takes attributes for website domains but the function can be edited to change the graph’s appearance.

The code

/* Add Alexa Graph Shortcode */
function jx_alexa_graph_shortcode( $atts ) {

    // Attributes
    extract( shortcode_atts(
        array(
            'first' => '',
            'second' => '',
            'third' => '',
        ), $atts )
    );

    // Code
    return '<p><script type="text/javascript"
src="http://widgets.alexa.com/traffic/javascript/graph.js"></script>
<script type="text/javascript">/*
<![CDATA[*/
// USER-EDITABLE VARIABLES
// enter up to 3 domains, separated by a space
var sites      = ["'.esc_html($first).'", "'.esc_html($second).'", "'.esc_html($third).'"];
var opts = {
width:      400,  // width in pixels (max 400)
height:     220,  // height in pixels (max 300)
type:       "r",  // "r" Reach, "n" Rank, "p" Page Views
range:      "3m", // "7d", "1m", "3m", "6m", "1y", "3y", "5y", "max"
bgcolor:    "e6f3fc" // hex value without "#" char (usually "e6f3fc")
};
// END USER-EDITABLE VARIABLES
AGraphManager.add( new AGraph(sites, opts) );
//]]></script></p>';
}
add_shortcode( 'alexa-graph', 'jx_alexa_graph_shortcode' );

2
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of

Useful post, presenting a good tool. For those who speak Portuguese or Spanish and are interested in business management, please visit http://www.quanticaconsultoria.com

I had never noticed before about Alexa. But by reading this, I’d probably try to learn more about Alexa. Thanks for your Information.

Free to your inbox

Join our mailing list to receive the latest news and updates from JournalXtra.

You have Successfully Subscribed!