Coder Social home page Coder Social logo

nek- / syliusanalyticsplugin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from setono/syliusanalyticsplugin

0.0 0.0 0.0 756 KB

Use Google Analytics to track visitors, purchases etc. in your Sylius store

License: MIT License

PHP 91.39% JavaScript 4.11% Gherkin 2.06% Twig 0.46% Shell 1.98%

syliusanalyticsplugin's Introduction

Sylius Analytics Plugin

Latest Version Latest Unstable Version Software License Build Status

This plugin adds Google Analytics tracking to your store. It injects the tags directly and hence does not depend on third party tools like Google Tag Manager.

Installation

Step 1: Download the plugin

This plugin uses the TagBagBundle to inject scripts onto your page.

Open a command console, enter your project directory and execute the following command to download the latest stable version of this plugin:

$ composer require setono/sylius-analytics-plugin

# Omit this line if you want to override layout.html.twig as described at https://github.com/Setono/TagBagBundle#usage
$ composer require setono/sylius-tag-bag-plugin

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the plugin

Then, enable the plugin by adding it to the list of registered plugins/bundles in config/bundles.php file of your project before (!) SyliusGridBundle:

<?php
$bundles = [
    Setono\TagBagBundle\SetonoTagBagBundle::class => ['all' => true],
    
    // Omit this line if you didn't install the SyliusTagBagPlugin in step 1
    Setono\SyliusTagBagPlugin\SetonoSyliusTagBagPlugin::class => ['all' => true],
    
    Setono\SyliusAnalyticsPlugin\SetonoSyliusAnalyticsPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];

Step 3: Configure plugin

# config/packages/_sylius.yaml
imports:
    # ...
    - { resource: "@SetonoSyliusAnalyticsPlugin/Resources/config/app/config.yaml" }
    # ...

Step 4: Import routing

# config/routes/setono_sylius_analytics.yaml
setono_sylius_analytics:
    resource: "@SetonoSyliusAnalyticsPlugin/Resources/config/routing.yaml"

Step 5: Update your database schema

$ php bin/console doctrine:migrations:diff
$ php bin/console doctrine:migrations:migrate

Step 6: Create a property

When you create a property in Google Analytics you receive a tracking id which looks something like UA-12345678-1.

Now create a new property in your Sylius shop by navigating to /admin/properties/new. Remember to enable the property and enable the channels you want to track.

Step 7: You're ready!

The events that are tracked are located in the EventListener folder. To make your tracking even better you should create event listeners listening to the builder events fired in i.e. the PurchaseSubscriber.

Let's say you use the Brand plugin and want to enrich the items tracked with the brand of the product. That would look like this:

<?php

declare(strict_types=1);

namespace App\EventListener;

use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface;
use Setono\SyliusAnalyticsPlugin\Builder\ItemBuilder;
use Setono\SyliusAnalyticsPlugin\Event\BuilderEvent;
use Sylius\Component\Core\Model\OrderItemInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;


final class AddBrandSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            BuilderEvent::class => [
                'addBrand',
            ],
        ];
    }

    public function addBrand(BuilderEvent $event): void
    {
        $builder = $event->getBuilder();
        if(!$builder instanceof ItemBuilder) {
            return;
        }
        
        $subject = $event->getSubject();
        
        if(!$subject instanceof OrderItemInterface) {
            return;
        }
        
        $product = $subject->getProduct();
        if(!$product instanceof BrandAwareInterface) {
            return;
        }
        
        $builder->setBrand($product->getBrand()->getName());
    }
}

Contribute

Ways you can contribute:

  • Translate messages and validators to your mother tongue
  • Create Behat tests that verifies the scripts are outputted on the respective pages
  • Create new event subscribers that handle Analytics events which are not implemented

Thank you!

syliusanalyticsplugin's People

Contributors

4c0n avatar arek31 avatar bitbager avatar davidromani avatar dependabot-preview[bot] avatar dependabot[bot] avatar loevgaard avatar mateuszry avatar nedac-sorbo avatar roshyo avatar sophiebb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.