Skip to content

Commit

Permalink
Fixes #3734, added 'external backlinks' and 'referrer domains' stats …
Browse files Browse the repository at this point in the history
…to SEO widget via Majestic SEO API.
  • Loading branch information
diosmosis committed Mar 25, 2013
1 parent 750b789 commit 513fe7e
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lang/en.php
Expand Up @@ -1285,6 +1285,8 @@
'SEO_Bing_IndexedPages' => 'Bing indexed pages',
'SEO_Dmoz' => 'DMOZ entries',
'SEO_SEORankingsFor' => 'SEO Rankings for %s',
'SEO_ExternalBacklinks' => 'External Backlinks',
'SEO_ReferrerDomains' => 'Referrer Domains',
'SitesManager_PluginDescription' => 'Websites Management in Piwik: Add a new Website, Edit an existing one, Show the JavaScript code to include on your pages. All the actions are also available through the API.',
'SitesManager_Sites' => 'Websites',
'SitesManager_TrackingTags' => 'Tracking code for %s',
Expand Down
18 changes: 16 additions & 2 deletions plugins/SEO/API.php
Expand Up @@ -36,16 +36,18 @@ static public function getInstance()
}

/**
* Get rank
* Returns SEO statistics for a URL.
*
* @param string $url URL to request Ranks for
* @param string $url URL to request SEO stats for
* @return Piwik_DataTable
*/
public function getRank( $url )
{
Piwik::checkUserHasSomeViewAccess();
$rank = new Piwik_SEO_RankChecker($url);

$linkToMajestic = Piwik_SEO_MajesticClient::getLinkForUrl($url);

$data = array(
'Google PageRank' => array(
'rank' => $rank->getPageRank(),
Expand All @@ -72,6 +74,18 @@ public function getRank( $url )
'logo' => 'plugins/SEO/images/whois.png',
'id' => 'domain-age',
),
Piwik_Translate('SEO_ExternalBacklinks') => array(
'rank' => $rank->getExternalBacklinkCount(),
'logo' => 'plugins/SEO/images/majesticseo.png',
'logo_link' => $linkToMajestic,
'id' => 'external-backlinks',
),
Piwik_Translate('SEO_ReferrerDomains') => array(
'rank' => $rank->getReferrerDomainCount(),
'logo' => 'plugins/SEO/images/majesticseo.png',
'logo_link' => $linkToMajestic,
'id' => 'referrer-domains',
),
);

// Add DMOZ only if > 0 entries found
Expand Down
95 changes: 95 additions & 0 deletions plugins/SEO/MajesticClient.php
@@ -0,0 +1,95 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik_Plugins
* @package Piwik_SEO
*/

/**
* Client for Majestic SEO's HTTP API.
*
* Hides the HTTP request sending logic.
*/
class Piwik_SEO_MajesticClient
{
const API_BASE = 'http://simpleapi.majesticseo.com/sapi/';
const API_KEY = 'ETHPYY'; // please only use this key within Piwik

/**
* Returns a URL that can be used to view all SEO data for a particular website.
*
* @param string $targetSiteUrl The URL of the website for whom SEO stats should be
* accessible for.
* @return string
*/
public static function getLinkForUrl( $targetSiteUrl )
{
$domain = @parse_url($targetSiteUrl, PHP_URL_HOST);
return "http://www.majesticseo.com/reports/site-explorer/summary/$domain?IndexDataSource=F";
}

/**
* Returns backlink statistics including the count of backlinks and count of
* referrer domains (domains with backlinks).
*
* This method issues an HTTP request and waits for it to return.
*
* @param string $siteDomain The domain of the website to get stats for.
* @param int $timeout The number of seconds to wait before aborting
* the HTTP request.
* @return array An array containing the backlink count and referrer
* domain count:
* array(
* 'backlink_count' => X,
* 'referrer_domains_count' => Y
* )
* If either stat is false, either the API returned an
* error, or the IP was blocked for this request.
*/
public function getBacklinkStats( $siteDomain, $timeout = 300 )
{
$apiUrl = $this->getApiUrl($method = 'GetBacklinkStats', $args = array(
'items' => '1',
'item0' => $siteDomain
));
$apiResponse = Piwik_Http::sendHttpRequest($apiUrl, $timeout);

$result = array(
'backlink_count' => false,
'referrer_domains_count' => false
);

$apiResponse = Piwik_Common::json_decode($apiResponse, $assoc = true);
if (!empty($apiResponse)
&& !empty($apiResponse['Data']))
{
$siteSeoStats = reset($apiResponse['Data']);

if (isset($siteSeoStats['ExtBackLinks'])
&& $siteSeoStats['ExtBackLinks'] !== -1)
{
$result['backlink_count'] = $siteSeoStats['ExtBackLinks'];
}

if (isset($siteSeoStats['RefDomains'])
&& $siteSeoStats['RefDomains'] !== -1)
{
$result['referrer_domains_count'] = $siteSeoStats['RefDomains'];
}
}

return $result;
}

private function getApiUrl( $method, $args = array() )
{
$args['sak'] = self::API_KEY;

$queryString = http_build_query($args);
return self::API_BASE.$method.'?'.$queryString;
}
}
34 changes: 34 additions & 0 deletions plugins/SEO/RankChecker.php
Expand Up @@ -21,6 +21,7 @@
class Piwik_SEO_RankChecker
{
private $url;
private $majesticInfo = null;

public function __construct($url)
{
Expand Down Expand Up @@ -170,6 +171,28 @@ public function getAge()
}
return false;
}

/**
* Returns the number backlinks that link to the current site.
*
* @return int
*/
public function getExternalBacklinkCount()
{
$majesticInfo = $this->getMajesticInfo();
return $majesticInfo['backlink_count'];
}

/**
* Returns the number of referrer domains that link to the current site.
*
* @return int
*/
public function getReferrerDomainCount()
{
$majesticInfo = $this->getMajesticInfo();
return $majesticInfo['referrer_domains_count'];
}

/**
* Returns the domain age archive.org lists for the current url
Expand Down Expand Up @@ -332,4 +355,15 @@ private function CheckHash($Hashnum)

return '7'.$CheckByte.$HashStr;
}

private function getMajesticInfo()
{
if ($this->majesticInfo === null)
{
$client = new Piwik_SEO_MajesticClient();
$this->majesticInfo = $client->getBacklinkStats($this->url);
}

return $this->majesticInfo;
}
}
Binary file added plugins/SEO/images/majesticseo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion plugins/SEO/templates/index.tpl
Expand Up @@ -23,7 +23,7 @@
<table cellspacing='2' style='margin:auto;line-height:1.5em;padding-top:10px'>
{foreach from=$ranks item=rank}
<tr>
<td><img style='vertical-align:middle;margin-right:6px;' src='{$rank.logo}' border='0' alt="{$rank.label}"> {$rank.label}
<td>{if !empty($rank.logo_link)}<a href="{$rank.logo_link}" target="_blank">{/if}<img style='vertical-align:middle;margin-right:6px;' src='{$rank.logo}' border='0' alt="{$rank.label}">{if !empty($rank.logo_link)}</a>{/if} {$rank.label}
</td><td>
<div style='margin-left:15px'>
{if isset($rank.rank)}{$rank.rank}{else}-{/if}
Expand Down

0 comments on commit 513fe7e

Please sign in to comment.