Low-code Integrations

Integrate Scoby Analytics with Shopware

To seamlessly integrate Scoby Analytics into Shopware, follow this guide to create a plugin that logs page views on the server side.

Prerequisites

You need your License Key and Salt to use Scoby Analytics in Laravel.

Environment Configuration

Ensure that your server environment is set up with the necessary environment variables for SCOBY_API_KEY and SCOBY_SALT. Shopware, similar to other PHP applications, can use environment variables set at the server level or through .env files.

  • If you're using a .env file in your Shopware root directory (which is common if you're working locally), add the following lines:

    SCOBY_API_KEY=your_api_key_here
    SCOBY_SALT=your_generated_salt_here
    

    Replace your_api_key_here with the actual Scoby Analytics License Key and your_generated_salt_here with the salt value.

  • If you're configuring this on a live server, you can typically set environment variables through the server control panel or by editing server configurations directly. The exact steps vary based on your hosting provider and server software.

Setting Up the Plugin

Naming the Plugin

Choose a technical name for your plugin in UpperCamelCase. To avoid name duplication, use a company prefix. For our purposes, we'll call the plugin SwagScobyAnalytics.

Plugin Creation

Generate the basic plugin structure:

bin/console plugin:create SwagScobyAnalytics

For manual creation, follow the Shopware documentation.

Configure the Plugin

Edit SwagScobyAnalytics.php inside the src directory:

<?php declare(strict_types=1);

namespace Swag\ScobyAnalytics;

use Shopware\Core\Framework\Plugin;
use Scoby\Analytics\Client;

class SwagScobyAnalytics extends Plugin
{
    public function boot(): void
    {
        $apiKey = getenv('SCOBY_API_KEY');
        $salt = getenv('SCOBY_SALT');

        $client = new Client($apiKey, $salt);
               
        $ipAddress = $request->getClientIp();
        $userAgent = $request->getHeader('User-Agent');
        $requestedUrl = $request->getFullRequestUri();
        $referringUrl = $request->getHeader('referer');
        
        $client
            ->setIpAddress($ipAddress)
            ->setUserAgent($userAgent)
            ->setRequestedUrl($requestedUrl)
            ->setReferringUrl($referringUrl)
            ->logPageViewAsync();
    }
}

The above code fetches the Scoby Analytics License Key and salt from environment variables and logs page views asynchronously.

Update composer.json

Update or create the composer.json in the plugin's root directory (custom/plugins/SwagScobyAnalytics/composer.json):

{
    "name": "swag/scoby-analytics",
    "description": "Shopware integration for Scoby Analytics.",
    "version": "1.0.0",
    "type": "shopware-platform-plugin",
    "license": "MIT",
    "authors": [
        {
            "name": "Your Name or Company"
        }
    ],
    "require": {
        "shopware/core": "6.5.*",
        "scoby/analytics": "*"
    },
    "extra": {
        "shopware-plugin-class": "Swag\\ScobyAnalytics\\SwagScobyAnalytics",
        "label": {
            "de-DE": "Scoby Analytics Integration",
            "en-GB": "Scoby Analytics Integration"
        }
    },
    "autoload": {
        "psr-4": {
            "Swag\\ScobyAnalytics\\": "src/"
        }
    }
}

Ensure you have included the scoby/analytics package as a requirement.

Install the Plugin

To refresh the list of known plugins:

php bin/console plugin:refresh

To install and activate:

php bin/console plugin:install --activate SwagScobyAnalytics

Conclusion

Your Shopware instance is now integrated with Scoby Analytics. Every page view will be logged on the server side, ensuring a robust analytics setup.

Note: Remember to set the environment variables SCOBY_API_KEY and SCOBY_SALT on your server or in your deployment process.

Further Reading

For support or queries related to Scoby Analytics, contact hello@scoby.io.

Previous
Drupal