Skip to content

Commit 986965f

Browse files
authored
[Experimental] Add campaign parameter support on events (#46)
2 parents 33d3f41 + 7ad24f1 commit 986965f

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

src/Campaign.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4;
4+
5+
use AlexWestergaard\PhpGa4\Facade\Type\CampaignType;
6+
7+
/**
8+
* EXPERIMENTAL MODEL
9+
* @version 1.1.3-beta
10+
*/
11+
class Campaign implements CampaignType
12+
{
13+
public function __construct(
14+
protected null|string $id = null,
15+
protected null|string $source = null,
16+
protected null|string $medium = null,
17+
protected null|string $name = null,
18+
protected null|string $term = null,
19+
protected null|string $content = null
20+
) {
21+
}
22+
23+
public function setId(null|string $id)
24+
{
25+
$this->id = trim($id);
26+
return $this;
27+
}
28+
29+
public function setSource(null|string $source)
30+
{
31+
$this->source = trim($source);
32+
return $this;
33+
}
34+
35+
public function setMedium(null|string $medium)
36+
{
37+
$this->medium = trim($medium);
38+
return $this;
39+
}
40+
41+
public function setName(null|string $name)
42+
{
43+
$this->name = trim($name);
44+
return $this;
45+
}
46+
47+
public function setTerm(null|string $term)
48+
{
49+
$this->term = urlencode(trim($term));
50+
return $this;
51+
}
52+
53+
public function setContent(null|string $content)
54+
{
55+
$this->content = trim($content);
56+
return $this;
57+
}
58+
59+
public function toArray(): array
60+
{
61+
return array_filter(
62+
[
63+
'campaign_id' => $this->id,
64+
'campaign_source' => $this->source,
65+
'campaign_medium' => $this->medium,
66+
'campaign_name' => $this->name,
67+
'campaign_term' => $this->term,
68+
'campaign_content' => $this->content,
69+
],
70+
fn ($val) => !empty($val) && strval($val) !== '0'
71+
);
72+
}
73+
74+
public static function new($id = null, $source = null, $medium = null, $name = null, $term = null, $content = null)
75+
{
76+
return new static($id, $source, $medium, $name, $term, $content);
77+
}
78+
}

src/Facade/Type/CampaignType.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace AlexWestergaard\PhpGa4\Facade\Type;
4+
5+
interface CampaignType
6+
{
7+
const CAMPAIGN_VARS = [
8+
'campaign_id',
9+
'campaign_source',
10+
'campaign_medium',
11+
'campaign_name',
12+
'campaign_term',
13+
'campaign_content',
14+
];
15+
16+
/**
17+
* Used to identify which campaign this referral references. Use campaign_id to identify a specific campaign.
18+
*
19+
* @var id
20+
* @param null|string $id eg "newsletter_category"
21+
*/
22+
public function setId(null|string $id);
23+
24+
/**
25+
* Use campaign_source to identify a search engine, newsletter name, or other source.
26+
*
27+
* @var source
28+
* @param null|string $id eg "newsletter"
29+
*/
30+
public function setSource(null|string $source);
31+
32+
/**
33+
* Use campaign_medium to identify a medium such as email or cost-per-click.
34+
*
35+
* @var medium
36+
* @param null|string $id eg "email" or "cpc"
37+
*/
38+
public function setMedium(null|string $medium);
39+
40+
/**
41+
* Used for keyword analysis. Use campaign_name to identify a specific product promotion or strategic campaign.
42+
*
43+
* @var name
44+
* @param null|string $id eg "Weekly Newsletter 2020w20"
45+
*/
46+
public function setName(null|string $name);
47+
48+
/**
49+
* Used for paid search. Use campaign_term to note the keywords for this ad.
50+
*
51+
* @var term
52+
* @param null|string $id eg "continue+reading+here"
53+
*/
54+
public function setTerm(null|string $term);
55+
56+
/**
57+
* Used for A/B testing and content-targeted ads. Use campaign_content to differentiate ads or links that point to the same URL.
58+
*
59+
* @var content
60+
* @param null|string $id eg "section1"
61+
*/
62+
public function setContent(null|string $content);
63+
64+
/**
65+
* Should return array of campaign object with variables
66+
*
67+
* @return array<string,array<string,null|string>>
68+
*/
69+
public function toArray(): array;
70+
}

src/Helper/EventHelper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AlexWestergaard\PhpGa4\Facade\Type\EventType;
66
use AlexWestergaard\PhpGa4\Facade\Type\DefaultEventParamsType;
7+
use AlexWestergaard\PhpGa4\Facade\Type\CampaignType;
78
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
89

910
abstract class EventHelper extends IOHelper implements EventType
@@ -14,6 +15,8 @@ abstract class EventHelper extends IOHelper implements EventType
1415
protected null|string $page_title;
1516
protected null|string $screen_resolution;
1617

18+
protected array $campaign = [];
19+
1720
public function setLanguage(string $lang)
1821
{
1922
$this->language = $lang;
@@ -57,6 +60,19 @@ public function setEventPage(DefaultEventParamsType $page)
5760
return $this;
5861
}
5962

63+
public function setCampaign(?CampaignType $campaign)
64+
{
65+
$this->campaign = [];
66+
67+
foreach ($campaign as $tag => $val) {
68+
if (!in_array($tag, CampaignType::CAMPAIGN_VARS)) {
69+
continue;
70+
}
71+
72+
$this->campaign[$tag] = $val;
73+
}
74+
}
75+
6076
public function toArray(): array
6177
{
6278
$return = [];
@@ -81,6 +97,13 @@ public function toArray(): array
8197

8298
$return['params'] = parent::toArray();
8399

100+
if (!empty($this->campaign)) {
101+
$return['params'] = array_replace(
102+
$return['params'],
103+
$this->campaign
104+
);
105+
}
106+
84107
return $return;
85108
}
86109

test/Unit/EventTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use AlexWestergaard\PhpGa4\Exception\Ga4Exception;
1010
use AlexWestergaard\PhpGa4\Exception\Ga4EventException;
1111
use AlexWestergaard\PhpGa4\Event;
12+
use AlexWestergaard\PhpGa4\Campaign;
1213
use AlexWestergaard\PhpGa4Test\TestCase;
1314

1415
final class EventTest extends TestCase
@@ -319,6 +320,29 @@ public function test_viewsearchresults()
319320
$this->assertEventFills($this->populateEventByFromArray(clone $event));
320321
}
321322

323+
public function test_can_pass_campaign_parameters()
324+
{
325+
$event = new Event\PageView;
326+
327+
/** @var Event\PageView */
328+
$event = $this->populateEventByMethod($event);
329+
330+
$event->setCampaign(
331+
Campaign::new(
332+
'newsletter_category',
333+
'newsletter',
334+
'email',
335+
'Weekly Newsletter 2020w20',
336+
'continue reading here',
337+
'section1'
338+
)
339+
);
340+
341+
$this->analytics->addEvent($event);
342+
343+
$this->assertNull($this->analytics->post());
344+
}
345+
322346
public function test_throw_name_missing()
323347
{
324348
$mock = new class extends Event\Refund

0 commit comments

Comments
 (0)