Code Monkey home page Code Monkey logo

googletagmanagerbundle's Introduction

GoogleTagManagerBundle

Software License Build Status Scrutinizer Quality Score Join the chat at https://gitter.im/GoogleTagManagerBundle/Lobby

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!

Requirements

2.x

  • PHP 5.3 and higher
  • Symfony 2.8 and higher

3.x

  • PHP 5.6 and higher
  • Symfony 2.8 and higher

Install

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

2.x

$ composer require xynnn/google-tag-manager-bundle "~2.0"

3.x

$ 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.

Step 2: Enable the Bundle

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(),
        );

        // ...
    }

    // ...
}

Step 3: Enable Google Tag Manager

Add the configuration to your yaml file. Please don't forget to adjust your Google Tag Manager Id.

Step 4: Insert the ViewHelper

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

Step 5: Fill up the DataLayer from Google Tag Manager (Optional)

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);

Configuration

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&gtm_preview=env-30&gtm_cookies_win=x

Authors

Philipp Bräutigam

... and Contributors!

License

Copyright (c) 2017 Philipp Bräutigam This repository is released under the GNU LGPL v3.0 license.

googletagmanagerbundle's People

Contributors

charlienaud avatar chrs-myrs avatar conciergerie-numerique avatar dnna avatar dsbe-ak avatar elhornair avatar fbourigault avatar gitter-badger avatar ialvesdasilva avatar jkabat avatar raurieres-ljsl avatar stefandoorn avatar tmirks avatar toooni avatar vasike avatar xynnn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

googletagmanagerbundle's Issues

Play nice with Nelmio/ApiDocBundle

Affected versions: 1.x & 2.x

ApiDocBundle (2.13.2) embeds full jQuery lib as js payload directly into the <head> section.
This JS code contains following:

...
(f.support.boxModel?"<!doctype html>":"")+"<html><body>"),cl.close()
...

So GoogleTagManagerListener class (1.0.5 / 2.1.0) do not respect that and inject GTM into JS code, which is not real <body>.

Solution is to update regex from '/<body\b[^>]*>/' to '/<\/head>\s*<body\b[^>]*>/'.

Unfortunately could not provide PR right now, maybe later this week, if it won't be fixed earlier.

Release 3.1.2 contains breaking changes

Following the merge of #49, it looks like release 3.1.2 was tagged directly on master, thus including in that release every change made for 4.x

I'm unable to open a PR to fix this, as it involves creating a new branch :

git checkout tags/3.1.1 -b 3.x
git cherry-pick f477eee

...then tagging that as 3.1.x

Is this acceptable to you?

Support for multiple environments

It's a great extension, but missing one key thing that we need, the support for multiple tag manager environments. I'd like to allign these with our test/staging environments.

Essentially these environments are signalled by adding some parameters to the URLs in both the header and footer. I wonder if we could set these somehow in the config so that they can be passed from environment variables.

&gtm_auth=&gtm_preview=env-33&gtm_cookies_win=x

Perhaps just a section for additional URL parameters so that it isn't tied to the current implementation?

The "google_tag_manager" extension is not enabled.

I'm using Twig 2.4.3, Twig Extensions 1.5.1 and Symfony 3.8.3. When autoAppend is enabled, I run into the following error:

Twig_Error_Runtime:
The "google_tag_manager" extension is not enabled.

  at vendor/twig/twig/lib/Twig/ExtensionSet.php:92
  at Twig_ExtensionSet->getExtension('google_tag_manager')
     (vendor/twig/twig/lib/Twig/Environment.php:622)
  at Twig_Environment->getExtension('google_tag_manager')
     (vendor/xynnn/google-tag-manager-bundle/EventListener/GoogleTagManagerListener.php:49)
  at Xynnn\GoogleTagManagerBundle\EventListener\GoogleTagManagerListener->onKernelResponse(object(FilterResponseEvent), 'kernel.response', object(TraceableEventDispatcher))

Empty data when using ESI (not the main request)

Bundle version : 2.4.2
Since the kernel.reset is released i've got a big issue when displaying data.
I'm using esi_render to get the template who will be displaying the tagmanager data. But as it's an ESI this is not the main request, so at the end a the main request, all data is reseted and when i print it with the esi request the data is empty.
What is the good practice to avoid loss of data between main and esi request in this case ? i can't avoid using esi as all my pages are cached but the datalayer part must not be cached ?

Separate helpers for head/body

I'm just implementing GTM and their instructions are to put the JS part as high as possible inside the and the NOSCRIPT part directly inside the .

Currently all goes into the . I'm not sure how this affects performance, but I can imagine it does a bit. I haven't researched how easy it would be to split this up, but it might be a good add-on.

Latest instructions: https://developers.google.com/tag-manager/quickstart

BC breaks with Symfony 2.8 & GTM bundle 2.4

Running Symfony 2.8.27, updated to xyNNN/GoogleTagManagerBundle 2.4, received the error:

[InvalidArgumentException]
  The service "listener.google_tag_manager" must be public as event listeners are lazy-loaded.

when booting the application (even post-update cache:clear hook wouldn't complete). Rolled back to 2.3 and all is ok.

HTML append breaks BinaryFileResponse due to default null content type

The Symfony\Component\HttpFoundation\BinaryFileResponse(); on default instantiation has a null content type, this bundle intercepts those files and tries to inject the tag manager into the file, correctly Symfony does not allow this and throws a 500 error:

Uncaught PHP Exception LogicException: "The content cannot be set on a BinaryFileResponse instance."

Maybe the logic here could be reviewed to not try to inject when the content type is null?

// only append to HTML responses
if (!in_array($event->getResponse()->headers->get('content-type'), array('text/html', null))) {
    return false;
}

If persist any kind of redirect, the data is lost

Seems this bundle doesn't care about situation when for example user paid something and we redirect to success page.
I think we need to use something like flash messages in symfony that will show one time the data.

What do you think ?

Release 2.1

I think our recent changes officially do not introduce BC, but the way of working for customers is a bit different and many changes have been made. 2.1 makes sense for me therefore. This version is still compatible with PHP 5.6.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.