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
+ use UnitedPrototype \GoogleAnalytics \Internals \Util ;
32
+
33
+ use DateTime ;
34
+
35
+ /**
36
+ * You should serialize this object and store it in e.g. the user database to keep it
37
+ * persistent for the same user permanently (similar to the "__umtz" cookie of
38
+ * the GA Javascript client).
39
+ */
40
+ class Campaign {
41
+
42
+ /**
43
+ * See self::TYPE_* constants, will be mapped to "__utmz" parameter.
44
+ *
45
+ * @see Internals\ParameterHolder::$__utmz
46
+ * @var string
47
+ */
48
+ protected $ type ;
49
+
50
+ /**
51
+ * Time of the creation of this campaign, will be mapped to "__utmz" parameter.
52
+ *
53
+ * @see Internals\ParameterHolder::$__utmz
54
+ * @var DateTime
55
+ */
56
+ protected $ creationTime ;
57
+
58
+ /**
59
+ * Response Count, will be mapped to "__utmz" parameter.
60
+ *
61
+ * Is also used to determine whether the campaign is new or repeated,
62
+ * which will be mapped to "utmcn" and "utmcr" parameters.
63
+ *
64
+ * @see Internals\ParameterHolder::$__utmz
65
+ * @see Internals\ParameterHolder::$utmcn
66
+ * @see Internals\ParameterHolder::$utmcr
67
+ * @var int
68
+ */
69
+ protected $ responseCount = 0 ;
70
+
71
+ /**
72
+ * Campaign ID, a.k.a. "utm_id" query parameter for ga.js
73
+ * Will be mapped to "__utmz" parameter.
74
+ *
75
+ * @see Internals\ParameterHolder::$__utmz
76
+ * @var int
77
+ */
78
+ protected $ id ;
79
+
80
+ /**
81
+ * Source, a.k.a. "utm_source" query parameter for ga.js.
82
+ * Will be mapped to "utmcsr" key in "__utmz" parameter.
83
+ *
84
+ * @see Internals\ParameterHolder::$__utmz
85
+ * @var string
86
+ */
87
+ protected $ source ;
88
+
89
+ /**
90
+ * Google AdWords Click ID, a.k.a. "gclid" query parameter for ga.js.
91
+ * Will be mapped to "utmgclid" key in "__utmz" parameter.
92
+ *
93
+ * @see Internals\ParameterHolder::$__utmz
94
+ * @var string
95
+ */
96
+ protected $ gClickId ;
97
+
98
+ /**
99
+ * DoubleClick (?) Click ID. Will be mapped to "utmdclid" key in "__utmz" parameter.
100
+ *
101
+ * @see Internals\ParameterHolder::$__utmz
102
+ * @var string
103
+ */
104
+ protected $ dClickId ;
105
+
106
+ /**
107
+ * Name, a.k.a. "utm_campaign" query parameter for ga.js.
108
+ * Will be mapped to "utmccn" key in "__utmz" parameter.
109
+ *
110
+ * @see Internals\ParameterHolder::$__utmz
111
+ * @var string
112
+ */
113
+ protected $ name ;
114
+
115
+ /**
116
+ * Medium, a.k.a. "utm_medium" query parameter for ga.js.
117
+ * Will be mapped to "utmcmd" key in "__utmz" parameter.
118
+ *
119
+ * @see Internals\ParameterHolder::$__utmz
120
+ * @var string
121
+ */
122
+ protected $ medium ;
123
+
124
+ /**
125
+ * Terms/Keywords, a.k.a. "utm_term" query parameter for ga.js.
126
+ * Will be mapped to "utmctr" key in "__utmz" parameter.
127
+ *
128
+ * @see Internals\ParameterHolder::$__utmz
129
+ * @var string
130
+ */
131
+ protected $ term ;
132
+
133
+ /**
134
+ * Ad Content Description, a.k.a. "utm_content" query parameter for ga.js.
135
+ * Will be mapped to "utmcct" key in "__utmz" parameter.
136
+ *
137
+ * @see Internals\ParameterHolder::$__utmz
138
+ * @var string
139
+ */
140
+ protected $ content ;
141
+
142
+
143
+ /**
144
+ * @const string
145
+ */
146
+ const TYPE_DIRECT = 'direct ' ;
147
+ /**
148
+ * @const string
149
+ */
150
+ const TYPE_ORGANIC = 'organic ' ;
151
+ /**
152
+ * @const string
153
+ */
154
+ const TYPE_REFERRAL = 'referral ' ;
155
+
156
+
157
+ /**
158
+ * @see createFromReferrer
159
+ * @param string $type See TYPE_* constants
160
+ */
161
+ public function __construct ($ type ) {
162
+ if (!in_array ($ type , array (self ::TYPE_DIRECT , self ::TYPE_ORGANIC , self ::TYPE_REFERRAL ))) {
163
+ Tracker::_raiseError ('Campaign type has to be one of the Campaign::TYPE_* constant values. ' , __METHOD__ );
164
+ }
165
+
166
+ $ this ->type = $ type ;
167
+
168
+ switch ($ type ) {
169
+ // See http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/campaign/CampaignManager.as#375
170
+ case self ::TYPE_DIRECT :
171
+ $ this ->name = '(direct) ' ;
172
+ $ this ->source = '(direct) ' ;
173
+ $ this ->medium = '(none) ' ;
174
+ break ;
175
+ // See http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/campaign/CampaignManager.as#340
176
+ case self ::TYPE_REFERRAL :
177
+ $ this ->name = '(referral) ' ;
178
+ $ this ->medium = 'referral ' ;
179
+ break ;
180
+ // See http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/campaign/CampaignManager.as#280
181
+ case self ::TYPE_ORGANIC :
182
+ $ this ->name = '(organic) ' ;
183
+ $ this ->medium = 'organic ' ;
184
+ break ;
185
+ }
186
+
187
+ $ this ->creationTime = new DateTime ();
188
+ }
189
+
190
+ /**
191
+ * @link http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/campaign/CampaignManager.as#333
192
+ * @param string $url
193
+ * @return \UnitedPrototype\GoogleAnalytics\Campaign
194
+ */
195
+ public static function createFromReferrer ($ url ) {
196
+ $ instance = new static (self ::TYPE_REFERRAL );
197
+ $ urlInfo = parse_url ($ url );
198
+ $ instance ->source = $ urlInfo ['host ' ];
199
+ $ instance ->content = $ urlInfo ['path ' ];
200
+
201
+ return $ instance ;
202
+ }
203
+
204
+ /**
205
+ * @link http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/campaign/CampaignTracker.as#153
206
+ */
207
+ public function validate () {
208
+ // NOTE: gaforflash states that id and gClickId must also be specified,
209
+ // but that doesn't seem to be correct
210
+ if (!$ this ->source ) {
211
+ Tracker::_raiseError ('Campaigns need to have at least the "source" attribute defined. ' , __METHOD__ );
212
+ }
213
+ }
214
+
215
+ /**
216
+ * @param string $type
217
+ */
218
+ public function setType ($ type ) {
219
+ $ this ->type = $ type ;
220
+ }
221
+
222
+ /**
223
+ * @return string
224
+ */
225
+ public function getType () {
226
+ return $ this ->type ;
227
+ }
228
+
229
+ /**
230
+ * @param DateTime $creationTime
231
+ */
232
+ public function setCreationTime (DateTime $ creationTime ) {
233
+ $ this ->creationTime = $ creationTime ;
234
+ }
235
+
236
+ /**
237
+ * @return DateTime
238
+ */
239
+ public function getCreationTime () {
240
+ return $ this ->creationTime ;
241
+ }
242
+
243
+ /**
244
+ * @param int $esponseCount
245
+ */
246
+ public function setResponseCount ($ responseCount ) {
247
+ $ this ->responseCount = (int )$ responseCount ;
248
+ }
249
+
250
+ /**
251
+ * @return int
252
+ */
253
+ public function getResponseCount () {
254
+ return $ this ->responseCount ;
255
+ }
256
+
257
+ /**
258
+ * @param int $byAmount
259
+ */
260
+ public function increaseResponseCount ($ byAmount = 1 ) {
261
+ $ this ->responseCount += $ byAmount ;
262
+ }
263
+
264
+ /**
265
+ * @param int $id
266
+ */
267
+ public function setId ($ id ) {
268
+ $ this ->id = $ id ;
269
+ }
270
+
271
+ /**
272
+ * @return int
273
+ */
274
+ public function getId () {
275
+ return $ this ->id ;
276
+ }
277
+
278
+ /**
279
+ * @param string $source
280
+ */
281
+ public function setSource ($ source ) {
282
+ $ this ->source = $ source ;
283
+ }
284
+
285
+ /**
286
+ * @return string
287
+ */
288
+ public function getSource () {
289
+ return $ this ->source ;
290
+ }
291
+
292
+ /**
293
+ * @param string $gClickId
294
+ */
295
+ public function setGClickId ($ gClickId ) {
296
+ $ this ->gClickId = $ gClickId ;
297
+ }
298
+
299
+ /**
300
+ * @return string
301
+ */
302
+ public function getGClickId () {
303
+ return $ this ->gClickId ;
304
+ }
305
+
306
+ /**
307
+ * @param string $dClickId
308
+ */
309
+ public function setDClickId ($ dClickId ) {
310
+ $ this ->dClickId = $ dClickId ;
311
+ }
312
+
313
+ /**
314
+ * @return string
315
+ */
316
+ public function getDClickId () {
317
+ return $ this ->dClickId ;
318
+ }
319
+
320
+ /**
321
+ * @param string $name
322
+ */
323
+ public function setName ($ name ) {
324
+ $ this ->name = $ name ;
325
+ }
326
+
327
+ /**
328
+ * @return string
329
+ */
330
+ public function getName () {
331
+ return $ this ->name ;
332
+ }
333
+
334
+ /**
335
+ * @param string $medium
336
+ */
337
+ public function setMedium ($ medium ) {
338
+ $ this ->medium = $ medium ;
339
+ }
340
+
341
+ /**
342
+ * @return string
343
+ */
344
+ public function getMedium () {
345
+ return $ this ->medium ;
346
+ }
347
+
348
+ /**
349
+ * @param string $term
350
+ */
351
+ public function setTerm ($ term ) {
352
+ $ this ->term = $ term ;
353
+ }
354
+
355
+ /**
356
+ * @return string
357
+ */
358
+ public function getTerm () {
359
+ return $ this ->term ;
360
+ }
361
+
362
+ /**
363
+ * @param string $content
364
+ */
365
+ public function setContent ($ content ) {
366
+ $ this ->content = $ content ;
367
+ }
368
+
369
+ /**
370
+ * @return string
371
+ */
372
+ public function getContent () {
373
+ return $ this ->content ;
374
+ }
375
+
376
+ }
377
+
378
+ ?>
0 commit comments