Skip to content

Commit 0b14c9c

Browse files
committed
Implemented social interaction tracking
1 parent a7438e6 commit 0b14c9c

File tree

5 files changed

+252
-1
lines changed

5 files changed

+252
-1
lines changed

src/GoogleAnalytics/Internals/ParameterHolder.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class ParameterHolder {
6060

6161
/**
6262
* Indicates the type of request, which is one of null (for page), "event",
63-
* "tran", "item", or "var"
63+
* "tran", "item", "social", "var" (deprecated) or "error" (used by ga.js
64+
* for internal client error logging).
6465
* @var string
6566
*/
6667
public $utmt;
@@ -449,6 +450,27 @@ class ParameterHolder {
449450
public $__utmz;
450451

451452

453+
// - - - - - - - - - - - - - - - - - Social Tracking parameters - - - - - - - - - - - - - - - - -
454+
455+
/**
456+
* The network on which the action occurs (e.g. Facebook, Twitter).
457+
* @var string
458+
*/
459+
public $utmsn;
460+
461+
/**
462+
* The type of action that happens (e.g. Like, Send, Tweet).
463+
* @var string
464+
*/
465+
public $utmsa;
466+
467+
/**
468+
* The page URL from which the action occurred.
469+
* @var string
470+
*/
471+
public $utmsid;
472+
473+
452474
// - - - - - - - - - - - - - - - - - Google Website Optimizer (GWO) parameters - - - - - - - - - - - - - - - - -
453475

454476
// TODO: Implementation needed

src/GoogleAnalytics/Internals/Request/Request.php

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ abstract class Request extends HttpRequest {
7171
* @const string
7272
*/
7373
const TYPE_ITEM = 'item';
74+
/**
75+
* @const string
76+
*/
77+
const TYPE_SOCIAL = 'social';
7478
/**
7579
* This type of request is deprecated in favor of encoding custom variables
7680
* within the "utme" parameter, but we include it here for completeness
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* Generic Server-Side Google Analytics PHP Client
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License (LGPL) as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19+
*
20+
* Google Analytics is a registered trademark of Google Inc.
21+
*
22+
* @link http://code.google.com/p/php-ga
23+
*
24+
* @license http://www.gnu.org/licenses/lgpl.html
25+
* @author Thomas Bachem <tb@unitedprototype.com>
26+
* @copyright Copyright (c) 2010 United Prototype GmbH (http://unitedprototype.com)
27+
*/
28+
29+
namespace UnitedPrototype\GoogleAnalytics\Internals\Request;
30+
31+
use UnitedPrototype\GoogleAnalytics\SocialInteraction;
32+
33+
class SocialinteractionRequest extends PageviewRequest {
34+
35+
/**
36+
* @var \UnitedPrototype\GoogleAnalytics\SocialInteraction
37+
*/
38+
protected $socialInteraction;
39+
40+
41+
/**
42+
* @return string
43+
*/
44+
protected function getType() {
45+
return Request::TYPE_SOCIAL;
46+
}
47+
48+
/**
49+
* @return \UnitedPrototype\GoogleAnalytics\Internals\ParameterHolder
50+
*/
51+
protected function buildParameters() {
52+
$p = parent::buildParameters();
53+
54+
$p->utmsn = $this->socialInteraction->getNetwork();
55+
$p->utmsa = $this->socialInteraction->getAction();
56+
$p->utmsid = $this->socialInteraction->getTarget();
57+
if($p->utmsid === null) {
58+
// Default to page path like ga.js,
59+
// see http://code.google.com/apis/analytics/docs/tracking/gaTrackingSocial.html#settingUp
60+
$p->utmsid = $this->page->getPath();
61+
}
62+
63+
return $p;
64+
}
65+
66+
/**
67+
* @return \UnitedPrototype\GoogleAnalytics\SocialInteraction
68+
*/
69+
public function getSocialInteraction() {
70+
return $this->socialInteraction;
71+
}
72+
73+
/**
74+
* @param \UnitedPrototype\GoogleAnalytics\SocialInteraction $socialInteraction
75+
*/
76+
public function setSocialInteraction(SocialInteraction $socialInteraction) {
77+
$this->socialInteraction = $socialInteraction;
78+
}
79+
80+
}
81+
82+
?>
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/**
4+
* Generic Server-Side Google Analytics PHP Client
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License (LGPL) as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19+
*
20+
* Google Analytics is a registered trademark of Google Inc.
21+
*
22+
* @link http://code.google.com/p/php-ga
23+
*
24+
* @license http://www.gnu.org/licenses/lgpl.html
25+
* @author Thomas Bachem <tb@unitedprototype.com>
26+
* @copyright Copyright (c) 2010 United Prototype GmbH (http://unitedprototype.com)
27+
*/
28+
29+
namespace UnitedPrototype\GoogleAnalytics;
30+
31+
class SocialInteraction {
32+
33+
/**
34+
* Required. A string representing the social network being tracked (e.g. "Facebook", "Twitter", "LinkedIn", ...),
35+
* will be mapped to "utmsn" parameter
36+
*
37+
* @see Internals\ParameterHolder::$utmsn
38+
* @var string
39+
*/
40+
protected $network;
41+
42+
/**
43+
* Required. A string representing the social action being tracked (e.g. "Like", "Share", "Tweet", ...),
44+
* will be mapped to "utmsa" parameter
45+
*
46+
* @see Internals\ParameterHolder::$utmsa
47+
* @var string
48+
*/
49+
protected $action;
50+
51+
/**
52+
* Optional. A string representing the URL (or resource) which receives the action. For example,
53+
* if a user clicks the Like button on a page on a site, the the target might be set to the title
54+
* of the page, or an ID used to identify the page in a content management system. In many cases,
55+
* the page you Like is the same page you are on. So if this parameter is not given, we will default
56+
* to using the path of the corresponding Page object.
57+
*
58+
* @see Internals\ParameterHolder::$utmsid
59+
* @var string
60+
*/
61+
protected $target;
62+
63+
64+
/**
65+
* @param string $path
66+
*/
67+
public function __construct($network = null, $action = null, $target = null) {
68+
if($network !== null) $this->setNetwork($network);
69+
if($action !== null) $this->setAction($action);
70+
if($target !== null) $this->setTarget($target);
71+
}
72+
73+
public function validate() {
74+
if($this->network === null || $this->action === null) {
75+
Tracker::_raiseError('Social interactions need to have at least the "network" and "action" attributes defined.', __METHOD__);
76+
}
77+
}
78+
79+
/**
80+
* @param string $network
81+
*/
82+
public function setNetwork($network) {
83+
$this->network = $network;
84+
}
85+
86+
/**
87+
* @return string
88+
*/
89+
public function getNetwork() {
90+
return $this->network;
91+
}
92+
93+
/**
94+
* @param string $action
95+
*/
96+
public function setAction($action) {
97+
$this->action = $action;
98+
}
99+
100+
/**
101+
* @return string
102+
*/
103+
public function getAction() {
104+
return $this->action;
105+
}
106+
107+
/**
108+
* @param string $target
109+
*/
110+
public function setTarget($target) {
111+
$this->target = $target;
112+
}
113+
114+
/**
115+
* @return string
116+
*/
117+
public function getTarget() {
118+
return $this->target;
119+
}
120+
121+
}
122+
123+
?>

src/GoogleAnalytics/Tracker.php

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use UnitedPrototype\GoogleAnalytics\Internals\Request\EventRequest;
3434
use UnitedPrototype\GoogleAnalytics\Internals\Request\TransactionRequest;
3535
use UnitedPrototype\GoogleAnalytics\Internals\Request\ItemRequest;
36+
use UnitedPrototype\GoogleAnalytics\Internals\Request\SocialInteractionRequest;
3637

3738
class Tracker {
3839

@@ -292,6 +293,25 @@ public function trackTransaction(Transaction $transaction, Session $session, Vis
292293
}
293294
}
294295

296+
/**
297+
* Equivalent of _trackSocial() in GA Javascript client.
298+
*
299+
* @link http://code.google.com/apis/analytics/docs/tracking/gaTrackingSocial.html#settingUp
300+
* @param \UnitedPrototype\GoogleAnalytics\SocialInteraction $socialInteraction
301+
* @param \UnitedPrototype\GoogleAnalytics\Page $page
302+
* @param \UnitedPrototype\GoogleAnalytics\Session $session
303+
* @param \UnitedPrototype\GoogleAnalytics\Visitor $visitor
304+
*/
305+
public function trackSocial(SocialInteraction $socialInteraction, Page $page, Session $session, Visitor $visitor) {
306+
$request = new SocialInteractionRequest(static::$config);
307+
$request->setSocialInteraction($socialInteraction);
308+
$request->setPage($page);
309+
$request->setSession($session);
310+
$request->setVisitor($visitor);
311+
$request->setTracker($this);
312+
$request->fire();
313+
}
314+
295315
/**
296316
* For internal use only. Will trigger an error according to the current
297317
* Config::$errorSeverity setting.

0 commit comments

Comments
 (0)