How to call a class within a function

Asked

Viewed 256 times

2

I have the following folder structure

/THEME
      /vendor
        autoload.php
        /composer
          files of composer
        /jflight
           /gacookie
              /src
              /tests
              other files
  composer.json
  composer.lock
  function.php

inside src, has the class:

Utmz.php

    <?php namespace Jflight\GACookie;

use Jflight\GACookie\ParseInterface;

class Utmz extends Cookie
{
    /**
     * Timestamp
     * @var string
     */
    public $timestamp;

    /**
     * Session count
     * @var string
     */
    public $session_count;

    /**
     * Campaign number
     * @var string
     */
    public $campaign_number;

    /**
     * Source
     * @var string
     */
    public $source;

    /**
     * Medium
     * @var string
     */
    public $medium;

    /**
     * Campaign
     * @var string
     */
    public $campaign;

    /**
     * Term
     * @var string
     */
    public $term;

    /**
     * Content
     * @var string
     */
    public $content;

    /**
     * Maps the string values in the cookie to the properties of the Utmz object
     * @var array
     */
    protected $keyMappings = array(
                        'utmcsr' => 'source',
                        'utmcmd' => 'medium',
                        'utmccn' => 'campaign',
                        'utmctr' => 'term',
                        'utmcct' => 'content'
                        );

    /**
     * Constructor
     * @param DateTimeImmutable $date
     */
    public function __construct(\DateTime $date)
    {
        $this->date = $date;
    }

    /**
     * Parse cookie string and assign properties to this
     * @param  string $cookie The string from inside the cookie
     * @return static
     */
    public function parse($cookie)
    {
        $cookieBits = explode('.', $cookie, 5);
        $this->timestamp = $this->date->createFromFormat('U', $cookieBits[1]);
        $this->session_count = (integer) $cookieBits[2];
        $this->campaign_number = (integer) $cookieBits[3];
        $array = $this->paramsToArray($cookieBits);
        $this->setExtras($array);
        return $this;
    }

    /**
     * Logic for setting the extras values from the cookie as object properties
     * @param array $array
     */
    protected function setExtras($array)
    {
        if (is_array($array))
        {
            foreach ($array as $key => $item)
            {
                if (array_key_exists($key, $this->keyMappings))
                {
                    $property = $this->keyMappings[$key];
                    $this->$property = $item;
                }
            }
        }
    }

    /**
     * Logic for splitting the extra properties from string to array
     * @param  string $cookie
     * @return array
     */
    protected function paramsToArray($cookie)
    {
        if (isset($cookie[4]))
        {

            $pairs = explode('|', $cookie[4]);

            foreach ($pairs as $pair)
            {
                $item = explode('=', $pair);

                $return[$item[0]] = $item[1];
            }

            return $return;
        } 
    }
}

in the archive function.php that this in the root added:

require_once dirname(__FILE__).'/vendor/autoload.php';

function get_data_cookies(){

            // COMO EU CHAMO A CLASSE AQUI, QUE ESTA NO DIRETÓRIO /src/Utmz.php???


            // utmz

            $utmz->timestamp; // DateTime
            $utmz->session_count; // Integer
            $utmz->campaign_number; // Integer
            $utmz->source; // string
            $utmz->medium; // string
            $utmz->campaign; // string
            $utmz->term; // string
            $utmz->content; // string

}

What I call the class within the function get_data_cookies?

----------------- UPDATING ----------

require get_template_directory().'/vendor/autoload.php';

use Jflight\GACookie\GACookie;


function get_cookies(){

    $utma = GACookie::parse('utma');
    $utmz = GACookie::parse('utmz');
    var_dump( $utma );
    var_dump( $utmz ); 

                    //utma

             $args = $utma->time_of_first_visit; // DateTime
             $args = $utma->time_of_last_visit; // DateTime
             $args = $utma->time_of_current_visit; // DateTime
             $args = $utma->session_count; // Integer

            // utmz

             $args = $utmz->timestamp; // DateTime
             $args = $utmz->session_count; // Integer
             $args = $utmz->campaign_number; // Integer
             $args = $utmz->source; // string
             $args = $utmz->medium; // string
             $args = $utmz->campaign; // string
             $args = $utmz->term; // string
             $args = $utmz->content;  // string


}

in the body of the contact form message where I am trying to use this entered data:

<?php get_cookies() ?>

but it returns

bool(false)bool(false)
  • I’m actually learning how to use I tried: use Jflight\GACookie\GACookie; gave error: Parse error: syntax error, unexpected 'use' (T_USE) in.. directory theme...functions.php on line 622

  • I’m trying to use this project in wordpress: https://github.com/jamesflight/Google-Analytics-Cookie-Parser-PHP

  • edited, put the correct class that should be called

  • Tried to put that line with the use at the beginning in the archive?

  • I tried like this, the mistake disappeared but when I try var_dump($utmz->source); returns NULL

1 answer

0

The library’s Utmz class Jflight/Gacookie needs to be instantiated and needs a string that represents the Google Analytics cookie to boot. I don’t know the context that is using this library, but try this:

require_once dirname(__FILE__).'/vendor/autoload.php';

use \Jflight\GACookie\Utmz;

function get_data_cookies(){
  $utmz = new Utmz(new Datetime());
  $utmz->parse("GA1.2.1640324400.1505291004.utmcct=testando|utmcsr=teste");  

  $utmz->timestamp; // DateTime
  $utmz->session_count; // Integer
  $utmz->campaign_number; // Integer
  $utmz->source; // string
  $utmz->medium; // string
  $utmz->campaign; // string
  $utmz->term; // string
  $utmz->content; // string
}

However, read the README.Md file from the library which has very clear use examples: https://github.com/jamesflight/Google-Analytics-Cookie-Parser-PHP

  • thanks for the help, I updated my question, I honestly did not understand very well what you did to stand twice ( one inside the other) Utmz and Datetime

  • I’m not really familiar with the idea behind Google Analytics cookies, but here’s a reading that should clarify: https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage. Comment on what you’re doing that I can try to help you more!

  • I created a contact form with basic fields for the user, but inside the body of the message I am trying to print when it occurs the click on Ubmit the contents of the variables $utmz and $utma, which are parsed cookies from Google Analytics, the most important thing for me right now is to know if the visitor came from organic or paid search. This article is very enlightening about Analytics cookies Debugging cookies and checking Google Analytics tags

  • But this data you can’t get in Google Analytics reports?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.