aawnu/php-ga4

Dashboard not reflecting the posted report

hablema opened this issue · 1 comments

I tried posting a pageview, and the code worked without a glitch, but the usage is not reported or shown in the analytics dashboard. Am I doing something wrong?

include_once(__DIR__ . '/vendor/autoload.php');

use AlexWestergaard\PhpGa4\Analytics;
use AlexWestergaard\PhpGa4\Event\PageView;

$clientId = $_COOKIE['_ga'] ?? $_COOKIE['_gid'] ?? $_COOKIE['PHPSESSID'] ?? null;

if(isset($_COOKIE['_ga'])) {
    $gaCookie = $_COOKIE['_ga'];
    $clientID = explode('.', $gaCookie)[2];
}


$event = PageView::new()
->setLanguage('en')
->setScreenResolution('1024x768')
->setPageLocation('/home')
->setPageTitle('Home');

// GA4 Initialize
$analytics = Analytics::new ('G-xxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxx')
->setClientId($clientId)
->setTimestampMicros(time())
->addEvent($event)
->post();

aawnu commented

When you post code you need 3x ` to make codeblock, single use if for "line" like oneline code while 3x is

a codeblock

I do not understand why you are splitting/exploding the cookie?

if(isset($_COOKIE['_ga'])) {
    $gaCookie = $_COOKIE['_ga'];
    $clientID = explode('.', $gaCookie)[2];
}

If you are using GTAG.js that initiate pageview then you do not need to send it in the backend also; this is more of a "fallback" solution to server side only usage (scraped data). It worked fine with me on a laravel project where I put it in a middleware to just send the client id and page.

Also if the event is sent 'as happens' you don't need the microtime, that is for delayed events, like a queue etc.