PHP code example of castrocrea / mixpanel-bundle

1. Go to this page and download the library: Download castrocrea/mixpanel-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

castrocrea / mixpanel-bundle example snippets


// config/bundles.php
    return [
        // ...
        Castrocrea\MixpanelBundle\CarstrocreaMixpanelBundle::class => ['all' => true],
    ];
}

// public/index.php
Request::setTrustedProxies(
    // the IP address (or range) of your proxy
    ['192.0.0.1', '10.0.0.0/8'],
    Request::HEADER_X_FORWARDED_ALL
);


// CheckoutController.php

use Castrocrea\MixpanelBundle\Annotation as Mixpanel;

class CheckoutController
{
    /**
     * @Mixpanel\Track("View Checkout")
     */
    public function view(Request $request)
    {
        // ...
    }


// UserController.php

use Castrocrea\MixpanelBundle\Annotation as Mixpanel;

class UserController
{
    /**
     * @Mixpanel\UpdateUser()
     */
    public function userUpdated(User $user, Request $request)
    {
        // ...
    }


// OrderController.php

use Castrocrea\MixpanelBundle\Annotation as Mixpanel;

class OrderController
{
    /**
     * @Mixpanel\Track("Order Completed", props={
     *      "user_id": @Mixpanel\Expression("user.getId()")
     * })
     * @Mixpanel\TrackCharge(
     *      id=324"),
     *      amount=@Mixpanel\Expression("order.getAmount()")
     * )
     */
    public function orderCompleted(Order $order, Request $request)
    {
        // ...
    }

/**
* @Mixpanel\Track("Your event", condition="request.getMethod() in ['GET']")
*/
public function yourAction()
{
  // ...
}

#In controller
namespace myNamespace;

use Castrocrea\MixpanelBundle\Annotation as Annotation;
use Castrocrea\MixpanelBundle\Mixpanel\Event\MixpanelEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

// ...

public function edit(User $user, EventDispatcherInterface $eventDispatcher, Request $request)
{
    // Your code
    $annotation = new Annotation\Track();
    $annotation->event = 'My event';
    $annotation->props = [
        'prop 1' => 'data 1',
        'prop 2' => 'data 2',
    ];
    
    $eventDispatcher->dispatch(new MixpanelEvent($annotation, $request));
    // Rest of your code
}

    /**
     * @Mixpanel\Track("Your event", props={
     *      "user_id": @Mixpanel\Expression("user.getId()")
     * })
     */
    public function yourAction()
    {
        // ...
    }

namespace YourNamespace;

use Doctrine\Common\Annotations\Reader;
use Castrocrea\MixpanelBundle\Annotation;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Security\Core\Security;

class MixpanelListener
{
    private $annotationReader;
    private $security;

    public function __construct(Reader $annotationReader, Security $security)
    {
        $this->annotationReader = $annotationReader;
        $this->security         = $security; 
    }

    public function onKernelController(ControllerEvent $event)
    {
        if (!\is_array($controller = $event->getController())) {
            return;
        }

        $className = \get_class($controller[0]);
        $object    = new \ReflectionClass($className);
        $method    = $object->getMethod($controller[1]);

        $classAnnotations  = $this->annotationReader->getClassAnnotations($object);
        $methodAnnotations = $this->annotationReader->getMethodAnnotations($method);

        foreach ([$classAnnotations, $methodAnnotations] as $collection) {
            foreach ($collection as $annotation) {
                if ($annotation instanceof Annotation\Annotation && property_exists($annotation, 'props')) {
                    $annotation->props['user_id'] = $this->security->getUser()->getId();
                }
            }
        }
    }
}
sh
$ php composer.phar