Skip to content

Instantly share code, notes, and snippets.

@davidfcarr
Created July 18, 2023 14:14
Show Gist options
  • Save davidfcarr/8f9249b6025ccaa4996a306400242278 to your computer and use it in GitHub Desktop.
Save davidfcarr/8f9249b6025ccaa4996a306400242278 to your computer and use it in GitHub Desktop.
WordPress Plugin for Similarweb Data Lookups
<?php
/*
Plugin Name: Dashboard Screen for Similarweb
*/
// see https://developer.wordpress.org/reference/
add_action('admin_menu','menu_for_similarweb');
function menu_for_similarweb() {
//anyone who can create blog posts can access this screen
add_menu_page( 'Similarweb', 'Similarweb', 'edit_posts', 'similarweb_screen', 'similarweb_screen_show');
}
function similarweb_screen_show() {
echo '<h1>Lookup Similarweb Data</h1>';
if(isset($_POST['api_key']))
{
$api_key = sanitize_text_field($_POST['api_key']);
update_option('similarweb_api_key',$api_key);
}
else
$api_key = get_option('similarweb_api_key');
if($api_key) {
$params = similarweb_api_describe($api_key);
if(isset($_POST))
similarweb_form_process($api_key);
similarweb_form($params);
}
similarweb_settings($api_key);
}
function similarweb_api_describe($api_key) {
//test for available data
$url = 'https://api.similarweb.com/v1/website/similarweb.com/traffic-and-engagement/describe';
$args = array();
// https://developer.wordpress.org/reference/functions/wp_remote_get/
$response = wp_remote_get( $url,$args );
if('200' != wp_remote_retrieve_response_code( $response ))
{
//handle error
echo '<div class="notice notice-error"><p>Connection failed</p></div>';
return array();
}
else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode($body);
return $data->response->traffic_and_engagement->countries->world;
}
}
function similarweb_lookup($lookup,$domain,$start,$end,$api_key,$country) {
//array of url strings with placeholders for variables
//todo allow for country as a variable
//todo get strings for unique_visitors and other desirable metrics from https://developers.similarweb.com/docs
$url_strings = array(
'visits' => 'https://api.similarweb.com/v1/website/%s/total-traffic-and-engagement/visits?api_key=%s&start_date=%s&end_date=%s&country=%s&granularity=monthly&main_domain_only=false&format=json&show_verified=false&mtd=false',
'unique_visitors' => '');
if(empty($url_strings[$lookup])) {
echo $lookup .' not found in '.var_export($url_strings,true);
return null;
}
else {
$url = sprintf($url_strings[$lookup],$domain,$api_key,$start,$end,$country);
if(isset($_POST['show_api_url']))
printf('<p>%s</p>',$url);
$response = wp_remote_get( $url,$args );
if('200' != wp_remote_retrieve_response_code( $response ))
{
//handle error
echo '<div class="notice notice-error"><p>Lookup failed for '.$domain.'</p></div>';
return array();
}
else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode($body);
return $data->$lookup;
}
}
}
function similarweb_form_process($api_key) {
if(isset($_POST['domains'])) {
$domains = explode("\n",$_POST['domains']);
$lookups = (isset($_POST['lookups'])) ? $_POST['lookups'] : array();
$start = sanitize_text_field($_POST['start']);
$end = sanitize_text_field($_POST['end']);
$country = sanitize_text_field($_POST['country']);
foreach($domains as $domain) {
$domain = trim($domain);
printf('<h2>%s</h2>',$domain);
if($domain) {
foreach($lookups as $lookup) {
printf('<h3>%s: %s</h3>',$lookup,$country);
$data = similarweb_lookup($lookup,$domain,$start,$end,$api_key,$country);
foreach($data as $item) {
//todo format or graph data as needed
//second variable will have to change, based on metric if not visits
printf('<p><strong>%s</strong> %s</p>',$item->date,intval($item->visits));
}
}
}
}
}
}
function similarweb_form($params) {
printf('<form method="post" action="%s">',admin_url('admin.php?page=similarweb_screen'));
?>
<p>Domains (1 per line)<br><textarea name="domains"></textarea></p>
<p>Start Year-Month <input type="text" name="start" value="<?php echo $params->start_date; ?>"> End Year-Month <input type="text" name="end" value="<?php echo $params->end_date; ?>"></p>
<p>Look up:</p>
<p><input type="checkbox" name="lookups[]" value="visits" checked="checked"> Visits</p>
<p><input type="checkbox" name="lookups[]" value="unique_visitors" > Unique Visitors (todo)</p>
<p><select name="country">
<option value="world">Worldwide</option>
<option value="us">United States</option>
<option value="gb">United Kingdom</option>
</select></p>
<p><input type="checkbox" name="show_api_url" value="1"> Show API string for debugging</p>
<?php
submit_button('Fetch');
echo '</form>';
}
function similarweb_settings($api_key) {
echo '<h3>Settings</h3>';
if($api_key)
echo '<p>API Key is set (you can reset it with the form below)</p>';
else
echo '<p>API Key must be set before you can look up Similarweb data</p>';
printf('<form method="post" action="%s">API KEY <input type="text" name="api_key"> <button>Submit</button></form>', admin_url('admin.php?page=similarweb_screen'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment