shineunited / tagmanager
Basic PHP container for Google Tag Manager
Installs: 5 675
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=5.6
- silex/silex: ^2.0
Requires (Dev)
- phpunit/phpunit: ~4.0|~5.0
- twig/twig: ^1.33
Suggests
- twig/twig: Allow use of tag manager inside of twig templates
README
A basic PHP abstraction for Google Tag Manager container and datalayer.
Installation
The recommended way to install Tag Manager is through Composer.
Install Composer
$ curl -sS https://getcomposer.org/installer | php
Add package to composer.json
$ composer require shineunited/tagmanager
Update dependencies
$ composer.phar update
Include autoloader
include(__DIR__ . '/../vendor/autoload.php');
Usage
Silex
To use Tag Manager with Silex, register the service provider
use ShineUnited\TagManager\Silex\TagManagerServiceProvider(); $app->register(new TagManagerServiceProvider(), [ 'gtm.options' => [ 'id' => 'GTM-XXXX', //gtm container id (required) 'persist' => true, //persist datalayer in session if true (optional, defaults to false) 'varname' => 'gtm' //session varname (optional, defaults to 'gtm') ] ]);
Adding messages to the datalayer
$app['gtm.datalayer']->push([ 'event' => 'gtm.eventName', 'eventData' => [ // event data goes here ] ]);
Twig
The extension adds the 'gtm()' function to the Twig environment. Note: the silex service provider will automatically install the twig extension if twig is present.
<html> <body> ... {{ gtm() }} </body> </html>
Or load the javascript and no-script snippets separately.
<html> <head> ... {{ gtm('head') }} </head> <body> {{ gtm('body') }} ... </body> </html>