Integration of the Mixpanel library into Symfony2.
Important: this bundle is developed in sync with Mixpanel's
repository. If you are using Mixpanel
2.6
, you need to use the ~2.6
releases of the bundle (or higher if available).
Require gordalina/mixpanel-bundle
using composer
$ php composer.phar require gordalina/mixpanel-bundle:~2.6
Or require
gordalina/mixpanel-bundle
to your composer.json
file:
{
"require": {
"gordalina/mixpanel-bundle": "@stable"
}
}
Protip: you should browse the
gordalina/mixpanel-bundle
page to choose a stable version to use, avoid the @stable
meta constraint.
Register the bundle in app/AppKernel.php
:
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Gordalina\MixpanelBundle\GordalinaMixpanelBundle(),
);
}
Enable the bundle's configuration in app/config/config.yml
:
# app/config/config.yml
gordalina_mixpanel:
projects:
default:
token: 794f0d43f9dbc91e3eb605a5a7bd37ce
This bundle registers a gordalina_mixpanel.default
, mixpanel.default
and mixpanel
service which is an instance of Mixpanel
(from the official library).
You'll be able to do whatever you want with it.
NOTE: This bundle sends your client's ip address automatically. If you have
a reverse proxy in you servers you should set it in your config.yml
:
# app/config/config.yml
framework:
trusted_proxies: ['127.0.0.1']
# ...
Track an event with a single annotation
<?php
// CheckoutController.php
use Gordalina\MixpanelBundle\Annotation as Mixpanel;
class CheckoutController
{
/**
* @Mixpanel\Track("View Checkout")
*/
public function viewAction(Request $request)
{
// ...
}
Mixpanel allows you to track your user's behaviours, but also some user information. When using annotations which require the distinct_id, this will be set automatically. This is done automatically provided you have configured it properly. You are able to override this value if you wish.
# app/config/config.yml
gordalina_mixpanel:
projects:
default:
token: 794f0d43f9dbc91e3eb605a5a7bd37ce
users:
Symfony\Component\Security\Core\User\UserInterface:
id: username
email: email
# All possible properties
YourAppBundle\Entity\User:
id: id
first_name: first_name
last_name: last_name
email: email
phone: phone
This bundle uses property access to get the values out of the user object, so
event if you dont have a first_name
property, but have a getFirstName
method
it will work.
<?php
// UserController.php
use Gordalina\MixpanelBundle\Annotation as Mixpanel;
class UserController
{
/**
* @Mixpanel\UpdateUser()
*/
public function userUpdatedAction(User $user, Request $request)
{
// ...
}
In the following example, we call UpdateUser, which sends all information registered
in the configuration above, but we override the id
property with an expression
that evaluates the user id.
The @Expression
annotation uses ExpressionLanguage
for evaluation.
<?php
// OrderController.php
use Gordalina\MixpanelBundle\Annotation as Mixpanel;
class OrderController
{
/**
* @Mixpanel\Track("Order Completed")
* @Mixpanel\TrackCharge(id=@Mixpanel\Expression("user.getId()"), amount=@Mixpanel\Expression("order.getAmount()"))
*/
public function orderCompletedAction(Order $order, Request $request)
{
// ...
}
Events
@Mixpanel\Register(prop="visits", value=3)
@Mixpanel\Track(event="name", props={ "firstTime": true })
@Mixpanel\Unregister(prop="email")
Engagement
@Mixpanel\Append(id=324, prop="fruits", value="apples" [, ignoreTime=false])
@Mixpanel\ClearCharges(id=324 [, ignoreTime=false])
@Mixpanel\DeleteUser(id=324 [, ignoreTime=false])
@Mixpanel\Increment(id=324, prop="visits", value=3 [, ignoreTime=false])
@Mixpanel\Remove(id=324, prop="email")
@Mixpanel\Set(id=324, props={ "firstTime": true } [, ignoreTime=false])
@Mixpanel\SetOnce(id=324, props={ "firstTime": true } [, ignoreTime=false])
@Mixpanel\TrackCharge(id=697, amount="20.0" [, ignoreTime=false])
@Mixpanel\Id()
@Mixpanel\Expression(expression="<expression>")
@Mixpanel\UpdateUser()
Note: The first argument is not required to specify the name explicitly,
e.g: @Mixpanel\Expression("<expression>")
or @Mixpanel\Set("<property>", value="<value>")
.
Note: All id
properties can be omitted, as they will be set with the id of
the current user in security.context
Mixpanel bundle additionally integrates with Symfony2 profiler. You can check number of events and engagements sent, total execution time and other information.
You'll find the reference configuration below:
# app/config/config*.yml
gordalina_mixpanel:
enable_profiler: %kernel.debug% # defaults to %kernel.debug%
projects:
default:
token: 794f0d43f9dbc91e3eb605a5a7bd37ce # required
options:
max_batch_size: 50 # the max batch size Mixpanel will accept is 50,
max_queue_size: 1000 # the max num of items to hold in memory before flushing
debug: false # enable/disable debug mode (logs messages to error_log)
consumer: curl # which consumer to use (curl, file, socket)
consumers:
custom_consumer: ConsumerStrategies_CustomConsumConsumer # Your consumer, update above to use it
host: api.mixpanel.com # the host name for api calls
events_endpoint: /track # host relative endpoint for events
people_endpoint: /engage # host relative endpoint for people updates
use_ssl: true # use ssl when available
error_callback: 'Doctrine\Common\Util\Debug::dump'
minimum_configuration:
token: 794f0d43f9dbc91e3eb605a5a7bd37ce
users:
Symfony\Component\Security\Core\User\UserInterface:
id: username
email: email
# All possible properties
YourAppBundle\Entity\User:
id: id
first_name: first_name
last_name: last_name
email: email
phone: phone
In order to run the specs install all components with composer and run:
./bin/phpspec run
This bundle is released under the MIT license. See the complete license in the bundle: