Youtube subscribers in google analytics

UPDATE: since YT api is in version no. 3 (june 2015) the example below is not working properly (I will do my best to update that solution)


 

There are some questions from time to time  in my company about our YT channel. One of the most common question is: how many subscribers do we have. Of course it can be always checked in YT analytics dashboard however, as my friend from Google said, YT analytics is an younger brother of google analytics. Younger so probably less experienced…

Of course I have my YT account connected with google analytics account however number of subscribers is not included in standard GA reports. I decided to change it. Of course I haven’t been the first one who wanted to do it.

That is how I did it.

      1. first of all you have to have access to google analytics (what is simple and free of course). I decided to created new property for my account (first property is dedicated to web page) which will contain YT data. Of course there is a lot information from channel collected automatically by google analytics  – not only number of subscribers.
        GA-analytics
        Properties for all social channels

        As you can see, my decision was to create new properties for all social channels. There is an information how to connect youtube with google analytics.

        Diagram below shows hierarchy in analytics accounts:

        GA hierarchy
        GA hierarchy
      2. How to treat info about subscribers? How to push it into GA reports? The best idea in my opinion is to push it as events. As you probably know we have 4 levels of events in GA:
        1. category ==> e.g. “my-new-metrics”
        2. action ==> e.g. “YT stats”
        3. label (optional, but recommended) ==> e.g.  “YT subscribers”
        4. value (optional) ==> and here should be daily number of subscribers

          Yes - it does work!
          Yes – it does work!
      3. Ok, so how to find numer of subscribers for our account? Of course we can use YT API and probably this is the best and the most effective way to do it. I decided, however, to use simply link with basic YT statistic in XML file. You can find there an information about ‘subscriberCount‘ but also about ‘totalUploadViews‘ and numbers of films in your channel.
$ytUsername= 'replace-with-yt-username';
$xmlData = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . strtolower($ytUsername));

Let’s do it

Ok, so we have all we need: google analytics access, data which should be push to analytics – the question is: how to do it? So that’s the best moment for our new friend – measurement protocol:

The Google Analytics Measurement Protocol allows developers to make HTTP requests to send raw user interaction data directly to Google Analytics servers. This allows developers to measure how users interact with their business from almost any environment. Developers can then use the Measurement Protocol to:

  • Measure user activity in new environments.
  • Tie online to offline behavior.
  • Send data from both the web and server.

Source: google

Finally I wrote some php code to make it working:

<?php
//error_reporting(E_ALL);

//------------------------------------------------------------------------------------------------
//PARAMETERS
//------------------------------------------------------------------------------------------------
$url = $v = $tid = $cid = $t = $ec = $ea = $el = $ev = '';

$ytUsername = 'replace-with-yt-username';
$url = 'http://www.google-analytics.com/collect';
$v = 1;
$tid = 'UA-11111111-1';		//your tracking ID
$cid = 666;
$t = 'event';
$ec = 'category-for-your-events';
$ea = 'acrion-for-your-events';
$el = array(
	'YT-subscribers-no',
	'YT-views-no',
	'YT-videos-no',
	);
//------------------------------------------------------------------------------------------------
//FUNCTIONS
//------------------------------------------------------------------------------------------------

//building parametrs string for payloadData

function payloadData($v, $tid, $cid, $t, $ec, $ea, $el, $ev){
	$data = array(
		'v' => $v,
		'tid' => $tid,
		'cid' => $cid,
		't' => $t,
		'ec' => $ec,
		'ea' => $ea,
		'el' => $el,
		'ev' => $ev
	);
	$payloadData = http_build_query($data);
	return($payloadData);
}

//------------------------------------------------------------------------------------------------

//fetching YT subscribers, views and videos no

function getYtStats($ytUsername) {
 $xmlData = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . strtolower($ytUsername));
 $xmlDataYt = str_replace('yt:', 'yt', $xmlData);
 $xmlYt = new SimpleXMLElement($xmlDataYt);
 $subs = $xmlYt->ytstatistics['subscriberCount'];
 $views = $xmlYt->ytstatistics['totalUploadViews']; 

 $xmlDataGd = str_replace('gd:', 'gd', $xmlData);
 $xmlGd = new SimpleXMLElement($xmlDataGd);

 $videos = $xmlGd->gdfeedLink[4]->attributes()->countHint;

 $ytStats = array(
 (int)$subs,
 (int)$views,
 (int)$videos,
 );

 return($ytStats);
}

//------------------------------------------------------------------------------------------------
//RUN
//------------------------------------------------------------------------------------------------

$ytStats = getYtStats($ytUsername);

$payloadData = array(
 payloadData($v, $tid, $cid, $t, $ec, $ea, $el[0], $ytStats[0]),
 payloadData($v, $tid, $cid, $t, $ec, $ea, $el[1], $ytStats[1]),
 payloadData($v, $tid, $cid, $t, $ec, $ea, $el[2], $ytStats[2]),
 );

foreach($payloadData as $value){
 $options = array(
 'http' => array(
 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
 'method' => 'POST',
 'content' => $value,
 ),
 );

 $context = stream_context_create($options);
 $result = file_get_contents($url, false, $context);
 var_dump($result);
}

Now all you have to do is to fire this script everyday. I created cron task which starts everyday at 12:30 and it works 🙂

Measurement Protocol hit
Measurement Protocol hit

Finally I created a dashboard to present data in one place.

Youtube dashboard
Youtube dashboard

Have a nice data collecting!

Leave a Reply

Your email address will not be published. Required fields are marked *