Download the PHP package anthonypauwels/datalayer without Composer
On this page you can find all versions of the php package anthonypauwels/datalayer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package datalayer
PHP DataLayer
A helper package that help to generate Google's DataLayer script on pages.
Installation
Require this package with composer.
Laravel without auto-discovery:
If you do not use auto-discovery, add the ServiceProvider to the providers array in config/app.php
:
Then add this line to your facades in config/app.php
:
Finally, add your GTM-ID in config/datalayer.php
.
Usage
Without Laravel
You must create a SessionHandler object. Handler uses session to pass data through pages. Then you can pass the SessionHandler to the DataLayerHandler using the constructor :
With Laravel
The package provides by default a Facade for Laravel application. You can call methods directly using the Facade or use the alias instead.
API documentation
Examples bellow are using Laravel Facade DataLayer
.
In your controllers
Push one value in the DataLayer
Push an array of data in the DataLayer
Do not hesitate to check the prototype of the method to view all possibles options.
In your views
Publish the DataLayer in the view
Just call this method in your app layout before the closing
tag.It will print this entire HTML code in your layout :
Do not forget to call DataLayer::noScript()
right after your
It will print the following :
You can use an optional array to choose if you do not want to initialise the global JS object, initialise the Google Tag Manager script or to clear data after publish.
It will just print this :
Blade directives
If you use DataLayerHandler with Laravel, you can use custom Blade directives to insert codes into the view without using the facade directly :
The DataLayer is cleared after each call to the DataLayer::publish() method except if the option clear
is set to false
.
Push an array of data in the DataLayer
Others methods
Load the data from session
Save the data in the session
Clear the data in the session
Get the array data
Print the global JS object in the view
It will print this in the HTML :
Print the Google Tag Manager script in the view
The $gtm_id
parameter is optional. If omitted, it will use the Google ID set in your .env file.
Also, do not forget to add the
Show the content of the DataLayer (debug purpose)
Requirement
PHP 8.0 or above