https://github.com/lexik/lexikdatalayerbundle
Manipulate the Google Tag Manager Data Layer from your Symfony application
https://github.com/lexik/lexikdatalayerbundle
google-tag-manager symfony symfony-bundle
Last synced: 10 months ago
JSON representation
Manipulate the Google Tag Manager Data Layer from your Symfony application
- Host: GitHub
- URL: https://github.com/lexik/lexikdatalayerbundle
- Owner: lexik
- Created: 2016-02-15T21:19:47.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-04-02T19:51:05.000Z (almost 7 years ago)
- Last Synced: 2025-05-20T06:17:50.857Z (10 months ago)
- Topics: google-tag-manager, symfony, symfony-bundle
- Language: PHP
- Size: 22.5 KB
- Stars: 14
- Watchers: 6
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
LexikDataLayerBundle
====================
[](https://packagist.org/packages/lexik/data-layer-bundle)
[](https://travis-ci.org/lexik/LexikDataLayerBundle)
[](https://scrutinizer-ci.com/g/lexik/LexikDataLayerBundle/?branch=master)
[](https://insight.sensiolabs.com/projects/4d91d8a5-3a7f-423b-9c6f-5c161246a891)
Manipulate the Google Tag Manager Data Layer from your Symfony application.
This bundle is compatible Symfony 2.7+, 3.0+ and 4.0+ and PHP 5.4+ to 7.0+.
Installation
------------
Add [`lexik/data-layer-bundle`](https://packagist.org/packages/lexik/data-layer-bundle)
to your `composer.json` file:
composer require "lexik/data-layer-bundle"
Register the bundle in `app/AppKernel.php`:
``` php
public function registerBundles()
{
return array(
// ...
new Lexik\Bundle\DataLayerBundle\LexikDataLayerBundle(),
);
}
```
Adding informations to the Data Layer
-------------------------------------
To pass informations to the Data Layer use the `lexik_data_layer.manager.data_layer_manager` service directly.
#### Event / Session Data
##### Use case
Notify an application event that could be used as goal or conversion like a user registration.
##### Usage
Get the service `lexik_data_layer.manager.data_layer_manager` and pass an associative array to it's `add()` method, it will be stored in session until it is passed to a page. Much like a Flash Message.
Using sessions as storage allows you to notify of an event even after a redirect for example.
Example usage from an EventListener to notify a user registration :
```php
manager = $manager;
}
/**
* onUserRegistration
*/
public function onUserRegistration()
{
$this->manager->add(['registration' => true]);
}
}
```
```xml
```
#### Default Data
##### Use case
Set the user id on every page for example.
##### Usage example
Create a service implementing the `Lexik\Bundle\DataLayerBundle\Collector\CollectorInterface` and tag it using the `lexik_data_layer.collector` tag.
It's `handle` method will be passed the current Data Layer array, which you can modify by adding or modifying its values.
```php
tokenStorage = $tokenStorage;
}
/**
* {@inheritdoc}
*/
public function handle(&$data)
{
$token = $this->tokenStorage->getToken();
if ($token->getUser() && $token->getUser() instanceof UserInterface) {
$data[] = ['user_id' => md5($token->getUser()->getUsername())];
}
}
}
```
```xml
```
## Adding / Writing Data Layer variables to the page
Use the provided `lexik_data_layer()` twig function to write the Data Layer value to a page template.
This will automatically reset the Data Layer informations stored in session.
Don't forget to use it BEFORE you insert the Tag Manager tag.
```twig
var dataLayer = {{ lexik_data_layer() }};
...
```
Testing
-------
Setup the test suite using [Composer](http://getcomposer.org/):
$ composer install
Run it using PHPUnit:
$ vendor/bin/phpunit
Contributing
------------
See [CONTRIBUTING](CONTRIBUTING.md) file.
Credits
-------
* Nicolas Cabot
* Lexik
* [All contributors](https://github.com/lexik/LexikDataLayerBundle/graphs/contributors)
License
-------
This bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE