2017 © Pedro Peláez
 

yii2-extension yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

image

mainaero/yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

  • Tuesday, January 30, 2018
  • by MainAero
  • Repository
  • 1 Watchers
  • 0 Stars
  • 89 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Yii2 GTM Widget

Build Status Maintainability Test Coverage, (*1)

A Yii2 extensions which provides a widget to render Google Tag Manager <script> and <noscript> snippets based on Yii2 params configuration. Further you can register it as component and use it to trigger dataLayer.push requests., (*2)

Install

Using composer:, (*3)

composer require "mainaero/yii2-gtm-widget"

Configuration

Add in your params-local.php file:, (*4)

'gtm_id' => '<YOUR_GTM_ID_WITHOUT_GTM_PREFIX> (required)',
'gtm_env' => '<YOUR_ENVIRONMENT_QUERY_STRING> (optional)'

E.g.:, (*5)

'gtm_id' => '1A2B3CD',
'gtm_env' => '&gtm_auth=<TOKEN>w&gtm_preview=<ENV_ID>&gtm_cookies_win=x'

If you don't set the gtm_id param this widget will return an empty string., (*6)

As Component

If you want to use it as component add following code to your main.php config:, (*7)

'components' => [
...
  'gtmDataLayerPush' => [
    'class' => 'mainaero\yii\gtm\component\DataLayerPush'
  ],
...
]

Usage

In your view file:, (*8)

<?php
use mainaero\yii\gtm\widget\GTM;
...

Render a <script> snippet:

<?= GTM::widget(); ?>

which renders:, (*9)

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl+'<YOUR_ENVIRONMENT_QUERY_STRING>';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-<YOUR_ID>');</script>
<!-- End Google Tag Manager -->

Render a <noscript> snippet:

<?= GTM::widget(['type' => 'noscript']); ?>

which renders:, (*10)

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-<YOUR_ID><YOUR_ENVIRONMENT_QUERY_STRING>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

Use to trigger dataLayer.push

In your controller add seomthing with:, (*11)

Yii::$app->gtmDataLayerPush->add(['event' => 'myEvent', 'eventCategory' => 'myCategory']);

Somewhere in your view or layout file output it with:, (*12)

<?= GTM::widget(['type' => 'dataLayerPush']) ?>

You can leave this snipped in your layout file as it will only render something if you have added something before., (*13)

The Versions

30/01 2018

dev-master

9999999-dev https://github.com/MainAero/yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar MainAero

yii2 google gtm

30/01 2018

v1.2.0

1.2.0.0 https://github.com/MainAero/yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar MainAero

yii2 google gtm

30/01 2018

v1.1.0

1.1.0.0 https://github.com/MainAero/yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar MainAero

yii2 google gtm

30/01 2018

v1.0.1

1.0.1.0 https://github.com/MainAero/yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar MainAero

yii2 google gtm

29/01 2018

v1.0.0

1.0.0.0 https://github.com/MainAero/yii2-gtm-widget

Google Tag Manager Widget for the Yii2 Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar MainAero

yii2 google gtm