PHP code example of dvsa / mot-google-analytics

1. Go to this page and download the library: Download dvsa/mot-google-analytics 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/ */

    

dvsa / mot-google-analytics example snippets




'modules' => [
    'Dvsa\Mot\Frontend\GoogleAnalyticsModule',
],



namespace MyModule\Controller;

use Dvsa\Mot\Frontend\GoogleAnalyticsModule\Package;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $view = new ViewModel([]);
        $view->setTemplate('my-module/index');

        $this->gtmDataLayer([
          'event'   => 'removed-defect',
          'journey' => 'RFRs',
          'title'   => 'Test result entry - the defect has been removed',
        ]);

        return $view;
    }
}



namespace MyModule\Service;

use Dvsa\Mot\Frontend\GoogleAnalyticsModule\TagManager\DataLayer;

class MyService
{
    /**
     * @var DataLayer
     */
    private $dataLayer;

    /**
     * To inject the $dataLayer object retrieve the `DataLayer::class` service from the main service container.
     *
     * @param $dataLayer DataLayer
     */
    public function __construct(DataLayer $dataLayer)
    {
        $this->dataLayer = $dataLayer;
        $this->dataLayer->add([
          'event'   => 'removed-defect',
          'journey' => 'RFRs',
          'title'   => 'Test result entry - the defect has been removed',
        ]);
    }
}

html
<html>
  <head>
    <script type="text/javascript">
       // Add variables and render the script all at once.
      echo $this->gtmDataLayer(['userId' => '5a713869ade8c366'])->render();
    


// Append variables
$this->gtmDataLayer(['userId' => '5a713869ade8c366']);