Various things we wrote

BLARG!

How to Programmatically Update Google Analytics Data

Do you need to programmatically manipulate Google Analytics data? You can do so via Google's Measurement Protocol.

Google provides a way to send data to Google Analytics through a REST API called the Measurement Protocol. It was particularly difficult to find this information, so I though I'd share it with you. 

The Measurement Protocol allows you to send custom dimensions, and metrics along with almost any other data GA tracks.

Learn about custom dimensions and metrics here.

Below is an example in PHP that sends a request to the API in debug mode. This request updates a custom dimension with the index of 1 and sends it along with a page view.

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// replace with your tracking id
$trackingId = 'UA-XXXX-2';
 
// the client id is the unique id google creates for a users group of sessions 
$clientId = '11111.11111';
 
// base url with tracking id
$baseUrl = 'https://www.google-analytics.com/debug/collect?v=1&tid='$trackingId;
 
// add client id (required for every request)
$baseUrl .= '&cid='.$clientId;
 
// add type of hit, the page it's hitting and set a custom dimensions value
$baseUrl .= '&t=pageview&df=%2F&cd1=foo';
 
// initialize curl
$ch = curl_init();
 
// set curl options
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $baseUrl
));
 
// send request
$response = curl_exec($ch);

Comments (0) Write a comment

Leave a Comment

828 582 4975

info@paleosun.com

70 Woodfin Place Suite 312
Asheville, NC, 28801