The GoogleTagManagerBundle provides you an easy-to-use method to integrate the Google Tag Manager into your Symfony 2 application.
Note: This Bundle is still in development. Feel free to report encountered issues!
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require xynnn/google-tag-manager-bundle "~2.0"
$ composer require xynnn/google-tag-manager-bundle "~3.0"
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding the following line in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Xynnn\GoogleTagManagerBundle\GoogleTagManagerBundle(),
);
// ...
}
// ...
}
Add the configuration to your yaml file. Please don't forget to adjust your Google Tag Manager Id.
Insert the ViewHelper into your layout file to enable the Google Tag Manager. Please be aware to insert into right after the HTML body tag!
<body>
{{ google_tag_manager_body() }}
...
</body>
And right after the HTML head tag:
<head>
{{ google_tag_manager_head() }}
...
</head>
And right before the closing BODY tag:
{{ google_tag_manager_body_end() }}
</body>
Or use the autoAppend
setting to let a kernel reponse listener add them to your layout automatically.
Additional instructions: https://developers.google.com/tag-manager/quickstart
If you want to send some information to the Google Tag Manager, you can use the dataLayer.
/** @var GoogleTagManagerInterface $manager */
$manager = $this->get('google_tag_manager');
$manager->setData('example', 'value');
And if you want to add pushes at the end of the body (not in initial dataLayer):
/** @var GoogleTagManagerInterface $manager */
$manager = $this->get('google_tag_manager');
$manager->addPush(['test' => 123);
google_tag_manager:
enabled: true
id: "GTM-XXXXXX"
autoAppend: true|false
An optional paramater called additionalParamaters
allows specifying URL parameters to add on to the tag manager URLs. This can be used to support custom environments in tag manager.
google_tag_manager:
additionalParameters: gtm_auth=XXXXXXXXXXXXX>m_preview=env-30>m_cookies_win=x
Philipp Bräutigam
... and Contributors!
Copyright (c) 2017 Philipp Bräutigam This repository is released under the GNU LGPL v3.0 license.