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
– Gislef
I’m trying to use this project in wordpress: https://github.com/jamesflight/Google-Analytics-Cookie-Parser-PHP
– Gislef
edited, put the correct class that should be called
– Gislef
Tried to put that line with the
use
at the beginning in the archive?– Woss
I tried like this, the mistake disappeared but when I try
var_dump($utmz->source);
returnsNULL
– Gislef