Skip to content

Instantly share code, notes, and snippets.

@zobzn
Created October 15, 2019 13:53
Show Gist options
  • Save zobzn/6e11f956c29d3085a1e939913a161a4e to your computer and use it in GitHub Desktop.
Save zobzn/6e11f956c29d3085a1e939913a161a4e to your computer and use it in GitHub Desktop.
Отправка данных в google analytics из php скрипта
<?php
/**
* @see https://developers.google.com/analytics/devguides/collection/protocol/v1/?hl=ru
* @see https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide?hl=ru
* @see https://developers.google.com/analytics/devguides/collection/protocol/v1/email?hl=ru
* @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters?hl=ru
*/
use Ramsey\Uuid\Uuid;
require_once __DIR__ . '/vendor/autoload.php';
$faker = Faker\Factory::create('ru_RU');
$track_url = 'https://www.google-analytics.com/collect';
// события (прочтение письма)
$data = [
'v' => 1,
't' => 'event',
'ec' => 'email', // категория событий
'ea' => 'open', // действие по событию
// 'el' => 'label', // ярлык события
'ev' => '3', // значение события
'tid' => 'UA-47099248-1',
'cid' => (string) Uuid::uuid4(),
'uip' => $faker->ipv4,
'dp' => '/ga-email',
];
// просмотры страниц
$data = [
'v' => 1,
// 'ds' => 'app',
// 'an' => 'zbz-tracker',
't' => 'pageview',
'tid' => 'UA-47099248-1',
'cid' => (string) Uuid::uuid4(),
'uip' => $faker->ipv4,
'dp' => '/ga-test',
'dt' => 'Тест аналитикса',
];
$client = new \GuzzleHttp\Client(['verify' => false]);
$request = new \GuzzleHttp\Psr7\Request('POST', $track_url, [], http_build_query($data));
$response = $client->send($request);
echo $track_url . PHP_EOL . PHP_EOL;
echo \GuzzleHttp\Psr7\str($request) . PHP_EOL . PHP_EOL;
echo \GuzzleHttp\Psr7\str($response) . PHP_EOL . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment