Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Create a custom wordpress plugin for semrush api traffic analytics


Create a custom wordpress plugin for semrush api traffic analytics


Here's an example of how you can create a custom WordPress plugin to integrate the Semrush API for traffic analytics:

1. Install the Semrush API client library:

First, you'll need to install the `silktide/semrush-api` library using Composer:

bash
composer require silktide/semrush-api

2. Create the WordPress plugin:

Create a new directory for your plugin, e.g., `semrush-traffic-analytics`, and add the following files:

- `semrush-traffic-analytics.php` (the main plugin file)
- `includes/class-semrush-traffic-analytics.php` (the main plugin class)

3. Implement the plugin class:

In the `includes/class-semrush-traffic-analytics.php` file, add the following code:

php
<?php
class Semrush_Traffic_Analytics {
    private $client;

    public function __construct($api_key) {
        $this->client = \Silktide\SemRushApi\ClientFactory::create($api_key);
    }

    public function get_domain_traffic($domain) {
        try {
            $result = $this->client->getDomainTraffic($domain, [
                'database' => \Silktide\SemRushApi\Data\Database::DATABASE_GOOGLE_US,
                'export_columns' => [
                    \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_VISITS,
                    \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_UNIQUE_USERS,
                    \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_BOUNCE_RATE,
                    \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_PAGES_PER_VISIT,
                    \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_AVG_VISIT_DURATION,
                    \Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_DESKTOP_MOBILE_RATIO,
                ]
            ]);

            return $result;
        } catch (\Silktide\SemRushApi\Exception\ApiException $e) {
            error_log('Semrush API error: ' . $e->getMessage());
            return null;
        }
    }
}

This class sets up the Semrush API client and provides a `get_domain_traffic()` method to retrieve the traffic analytics data for a given domain.

4. Implement the main plugin file:

In the `semrush-traffic-analytics.php` file, add the following code:

php
<?php
/*
Plugin Name: Semrush Traffic Analytics
Plugin URI: https://example.com/semrush-traffic-analytics
Description: Integrate Semrush API for traffic analytics in WordPress
Version: 1.0
Author: Your Name
Author URI: https://example.com
*/

require_once plugin_dir_path(__FILE__) . 'includes/class-semrush-traffic-analytics.php';

function semrush_traffic_analytics_init() {
    $api_key = 'your_semrush_api_key';
    $plugin = new Semrush_Traffic_Analytics($api_key);

    // Example usage
    $traffic_data = $plugin->get_domain_traffic('example.com');
    if ($traffic_data) {
        foreach ($traffic_data as $row) {
            $visits = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_VISITS);
            $unique_users = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_UNIQUE_USERS);
            $bounce_rate = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_BOUNCE_RATE);
            $pages_per_visit = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_PAGES_PER_VISIT);
            $avg_visit_duration = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_AVG_VISIT_DURATION);
            $desktop_mobile_ratio = $row->getValue(\Silktide\SemRushApi\Data\Column::COLUMN_OVERVIEW_DESKTOP_MOBILE_RATIO);

            // Display the data in the WordPress admin area
            echo "Visits: $visits, Unique Users: $unique_users, Bounce Rate: $bounce_rate, Pages per Visit: $pages_per_visit, Avg. Visit Duration: $avg_visit_duration, Desktop/Mobile Ratio: $desktop_mobile_ratio";
        }
    }
}

add_action('init', 'semrush_traffic_analytics_init');

This main plugin file sets up the `Semrush_Traffic_Analytics` class, passes the Semrush API key, and calls the `get_domain_traffic()` method to retrieve and display the traffic analytics data.

5. Replace `'your_semrush_api_key' with your actual Semrush API key.

6. Activate the plugin in the WordPress admin area.

Once the plugin is activated, it will display the traffic analytics data for the example.com domain in the WordPress admin area. You can customize the plugin to display the data in a more user-friendly way, such as creating a custom admin page or widget.

Remember to handle any API errors and consider caching the API responses to improve performance.

Citations:
[1] https://www.semrush.com/kb/5-api
[2] https://www.semrush.com/kb/972-api-reports
[3] https://github.com/silktide/semrush-api
[4] https://developer.semrush.com/api/basics/how-to-get-api/
[5] https://backlinko.com/hub/seo/semrush