shineunited/tagmanager

Basic PHP container for Google Tag Manager

2.0.0 2017-05-30 19:05 UTC

This package is auto-updated.

Last update: 2024-12-15 01:36:00 UTC


README

A basic PHP abstraction for Google Tag Manager container and datalayer.

Latest Stable Version Minimum PHP Version Build Status

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>