PHP code example of kairos / googleanalyticsserversidebundle

1. Go to this page and download the library: Download kairos/googleanalyticsserversidebundle 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/ */

    

kairos / googleanalyticsserversidebundle example snippets

 bash
    php composer.phar update kairos/googleanalyticsserversidebundle
 php
    
    // app/AppKernel.php

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Kairos\GoogleAnalyticsServerSideBundle\KairosGoogleAnalyticsServerSideBundle(),
        );
    }
 php

    // Call tracker from container
    $this->container->get('ga_mp_tracker')->track('pageview',
        array(
            'dp' => 'your.address.com',
            'dt' => 'your title'
        )
    );

    // Track event
    $this->container->get('ga_mp_tracker')->track('event',
        array(
            'dp' => 'your.address.com',
            'dt' => 'your title',
            'ec' => 'event category',
            'ea' => 'event action',
            'el' => 'event label',
        )
    );
 php

    // Initilize GA Tracker
    $tracker = $this->get('google_analytics');
    
    // Assemble Visitor information (could also get unserialized from database)
    $visitor = new GoogleAnalytics\Visitor();
    $visitor->setIpAddress($_SERVER['REMOTE_ADDR']);
    $visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);
    $visitor->setScreenResolution('1024x768');
    
    // Assemble Session information (could also get unserialized from PHP session)
    $session = new GoogleAnalytics\Session();
    
    // Assemble Page information
    $page = new GoogleAnalytics\Page('/page.html');
    $page->setTitle('My Page');
    
    // Track page view
    $tracker->trackPageview($page, $session, $visitor);