PHP code example of gordalina / mixpanel-bundle

1. Go to this page and download the library: Download gordalina/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/ */

    

gordalina / mixpanel-bundle example snippets


// config/bundles.php
    return [
        // ...
        Gordalina\MixpanelBundle\GordalinaMixpanelBundle::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_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO
);


// CheckoutController.php

use Gordalina\MixpanelBundle\Attribute as Mixpanel;

class CheckoutController
{
    #[Mixpanel\Track('View Checkout')]
    public function view(Request $request)
    {
        // ...
    }


// UserController.php

use Gordalina\MixpanelBundle\Attribute as Mixpanel;

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


// OrderController.php

use Gordalina\MixpanelBundle\Attribute as Mixpanel;

class OrderController
{
    #[Mixpanel\Track('Order Completed', props: [
        'user_id' => new Mixpanel\Expression('user.getId()'),
    ])]
    #[Mixpanel\TrackCharge(
        amount: new Mixpanel\Expression('order.getAmount()'),
        id: 324,
    )]
    public function orderCompleted(Order $order, Request $request)
    {
        // ...
    }

#[Mixpanel\Set(props: ['plan' => 'pro'], id: new Mixpanel\Id())]

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

#In controller
namespace myNamespace;

use Gordalina\MixpanelBundle\Attribute as Mixpanel;
use Gordalina\MixpanelBundle\Mixpanel\Event\MixpanelEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

// ...

public function edit(User $user, EventDispatcherInterface $eventDispatcher, Request $request)
{
    // Your code
    $attribute = new Mixpanel\Track('My event', [
        'prop 1' => 'data 1',
        'prop 2' => 'data 2',
    ]);

    $eventDispatcher->dispatch(new MixpanelEvent($attribute, $request));
    // Rest of your code
}

    #[Mixpanel\Track('Your event', props: [
        'user_id' => new Mixpanel\Expression('user.getId()'),
    ])]
    public function yourAction()
    {
        // ...
    }

namespace YourNamespace;

use Gordalina\MixpanelBundle\Attribute;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpKernel\Event\ControllerEvent;

class MixpanelListener
{
    public function __construct(private Security $security)
    {
    }

    public function onKernelController(ControllerEvent $event): void
    {
        foreach ($event->getAttributes() as $attributes) {
            foreach ($attributes as $attribute) {
                if ($attribute instanceof Attribute\Action && property_exists($attribute, 'props')) {
                    $attribute->props['user_id'] = $this->security->getUser()->getId();
                }
            }
        }
    }
}
sh
$ php composer.phar