PHP code example of cyber-duck / laravel-google-tag-manager
1. Go to this page and download the library: Download cyber-duck/laravel-google-tag-manager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cyber-duck / laravel-google-tag-manager example snippets
return [
/*
* The Google Tag Manager id, should be a code that looks something like "gtm-xxxx".
*/
'id' => '',
/*
* Enable or disable script rendering. Useful for local development.
*/
'enabled' => true,
/*
* If you want to use some macro's you 'll probably store them
* in a dedicated file. You can optionally define the path
* to that file here and we will load it for you.
*/
'macroPath' => '',
];
// HomeController.php
public function index()
{
GoogleTagManager::set('pageType', 'productDetail');
return view('home');
}
// ContactController.php
public function getContact()
{
GoogleTagManager::set('pageType', 'contact');
return view('contact');
}
public function postContact()
{
// Do contact form stuff...
GoogleTagManager::flash('formResponse', 'success');
return redirect()->action('ContactController@getContact');
}
// Retrieve your Google Tag Manager id
$id = GoogleTagManager::id(); // GTM-XXXXXX
// Check whether script rendering is enabled
$enabled = GoogleTagManager::isEnabled(); // true|false
// Enable and disable script rendering
GoogleTagManager::enable();
GoogleTagManager::disable();
// Add data to the data layer (automatically renders right before the tag manager script). Setting new values merges them with the previous ones. Set also supports dot notation.
GoogleTagManager::set(['foo' => 'bar']);
GoogleTagManager::set('baz', ['ho' => 'dor']);
GoogleTagManager::set('baz.ho', 'doorrrrr');
// [
// 'foo' => 'bar',
// 'baz' => ['ho' => 'doorrrrr']
// ]
$dataLayer = new Spatie\GoogleTagManager\DataLayer();
$dataLayer->set('ecommerce.click.products', $products->toJson());
echo $dataLayer->toJson(); // {"ecommerce":{"click":{"products":"..."}}}