PHPackages                             mainaero/yii2-gtm-widget - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mainaero/yii2-gtm-widget

ActiveYii2-extension[Framework](/categories/framework)

mainaero/yii2-gtm-widget
========================

Google Tag Manager Widget for the Yii2 Framework

v1.2.0(8y ago)024.3k↓46.3%MITPHP

Since Jan 29Pushed 8y ago1 watchersCompare

[ Source](https://github.com/MainAero/yii2-gtm-widget)[ Packagist](https://packagist.org/packages/mainaero/yii2-gtm-widget)[ Docs](https://github.com/MainAero/yii2-gtm-widget)[ RSS](/packages/mainaero-yii2-gtm-widget/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

Yii2 GTM Widget
===============

[](#yii2-gtm-widget)

[![Build Status](https://camo.githubusercontent.com/c0ba1c133254a7c7ee1bdd20b786a50bf5d880cde2625514a5f33fa1e58bcafe/68747470733a2f2f7472617669732d63692e6f72672f4d61696e4165726f2f796969322d67746d2d7769646765742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/MainAero/yii2-gtm-widget) [![Maintainability](https://camo.githubusercontent.com/f98a3c9e38ec7b6748748119da79d83c289c8b5cab3935ce88e0965e1cdab8f7/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f63656434313363633737353464653264376331322f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/MainAero/yii2-gtm-widget/maintainability) [![Test Coverage](https://camo.githubusercontent.com/39b69d76d2bfb54cbc5798558dedf24a575b1996185c69c1969abb3779220866/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f63656434313363633737353464653264376331322f746573745f636f766572616765)](https://codeclimate.com/github/MainAero/yii2-gtm-widget/test_coverage)

A Yii2 extensions which provides a widget to render Google Tag Manager `` and `` snippets based on Yii2 params configuration. Further you can register it as component and use it to trigger `dataLayer.push` requests.

Install
-------

[](#install)

Using composer:

```
composer require "mainaero/yii2-gtm-widget"
```

Configuration
-------------

[](#configuration)

Add in your `params-local.php` file:

```
'gtm_id' => ' (required)',
'gtm_env' => ' (optional)'
```

E.g.:

```
'gtm_id' => '1A2B3CD',
'gtm_env' => '&gtm_auth=w&gtm_preview=&gtm_cookies_win=x'
```

If you don't set the `gtm_id` param this widget will return an empty string.

### As Component

[](#as-component)

If you want to use it as component add following code to your `main.php` config:

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

Usage
-----

[](#usage)

In your view file:

```

```

which renders:

```

(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+'';f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-');

```

### Render a `` snippet:

[](#render-a-noscript-snippet)

```
