stonegrizzly
stonegrizzly

Reputation: 229

Custom user agents with google analytics

I'm sending requests to google analytics from an iOS app using the GA measurement protocol.

I'd like to be able to create a segment for all things mobile, which would include hits from the mobile web site as well the app. I can't figure out how to tell GA that my custom user agent corresponds to a mobile device (and an OS version, app version, resolution, etc).

Is there a way to map these custom user agents to devices? Or is there some sort of standard user agent that I could use that would convey this information?

Upvotes: 1

Views: 1140

Answers (1)

tklodd
tklodd

Reputation: 1079

If you just want to tell whether a pageview came from a website or an app, here's the reference: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds

And here's the parameter to use for resolution: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#sr

I don't think there are any default parameters that cover app version or operating system, but you could do custom dimensions for those: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cd_

So in php, you would do something like this:

$url = "www.google-analytics.com/collect";

function request($url, $post_fields) {
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec( $ch );
    return $response;
}

$post_fields = array(
    "v" => 1,  //version
    "tid" => $tid,  //tracking id
    "cid" => $cid,  //client id
    "t" => "pageview",  //hit type
    "ds" => $ds,  //data source
    "sr" => $sr, //screen resolution
    "cd1" => $cd1,  //custom dimension 1
    "cd2" => $cd2,  //custom dimension 2
    "cd3" => $cd3,  //custom dimension 3
);

$post_fields = http_build_query($post_fields);
request($this->url, $post_fields);

And of course, you would add in whatever other parameters you wanted to the post_fields array. Then you also need to go into Analytics and register your custom dimensions under Admin > Property > Custom Definitions. Also, you'll want to check the CURLOPT values to make sure they are what you want.

Upvotes: 1

Related Questions