Skip to content

Commit

Permalink
amzn program per country
Browse files Browse the repository at this point in the history
:(
  • Loading branch information
Tom Cannaerts committed Nov 13, 2024
1 parent 4fe62ee commit 76b0381
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
21 changes: 12 additions & 9 deletions src/Service/UrlTransformerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ class UrlTransformerService
* Known hostformats.
* key is regex to match host portion of url (parse_url)
* value are <type>[,parameters...] where type denotes the affiliate program later used for the transformation logic
* value can contain backreferences \1, \2, ... to the matches in the regular expression
*/
private array $hostFormats = [
'/^(www\.)?amazon\.(com|co\.(jp|uk|za)|com\.(au|be|br|mx|tr)|ae|ca|cn|de|eg|es|fr|ie|in|it|nl|pl|sa|se|sg)$/' => 'amazon',
'/^(www\.)?bol\.com$/' => 'bol',
'/^(www\.)?coolblue\.be$/' => 'awin,85165',
'/^(?:www\.)?amazon\.(com|ae|ca|cn|de|eg|es|fr|ie|in|it|nl|pl|sa|se|sg)$/' => 'amazon_\1',
'/^(?:www\.)?amazon\.co\.(jp|uk|za)$/' => 'amazon_co_\1',
'/^(?:www\.)?amazon\.com\.(au|be|br|mx|tr)$/' => 'amazon_com_\1',
'/^(?:www\.)?bol\.com$/' => 'bol',
'/^(?:www\.)?coolblue\.be$/' => 'awin,85165',
];

private $partnerIds = [];
Expand Down Expand Up @@ -96,7 +99,7 @@ public function transformUrl(string $url): string
$matchedFormat = '';
foreach ($this->hostFormats as $hostFormat => $key) {
if (preg_match($hostFormat, $urlParts['host'])) {
$matchedFormat = $key;
$matchedFormat = preg_replace($hostFormat, $key, $urlParts['host']);
break;
}
}
Expand All @@ -114,8 +117,8 @@ public function transformUrl(string $url): string
$partnerId = $this->partnerIds[$key][0];
}

switch ($key) {
case 'amazon':
switch (true) {
case 'amazon_' == substr($key, 0, 7):
// append id as tag parameter
if (isset($urlParts['query'])) {
$url .= '&tag='.$partnerId;
Expand All @@ -124,20 +127,20 @@ public function transformUrl(string $url): string
}
break;

case 'bol':
case 'bol' == $key:
// generate text link to partner program and append original URL encoded
$url = 'https://partner.bol.com/click/click?p=1&t=url&s='.$partnerId.'&f=TXL&url='.urlencode($url);
break;

case 'tradetracker':
case 'tradetracker' == $key:
// params[0] should contain campaignid, append original URL encoded
$url = 'https://tc.tradetracker.net/?c='.$params[0].'&m=12&a='.$partnerId.'&r=&u='.urlencode($urlParts['path']);
if (isset($urlParts['query'])) {
$url .= urlencode('?'.$urlParts['query']);
}
break;

case 'awin':
case 'awin' == $key:
// params[0] should contain merchantid, append original URL encoded
$url = 'https://www.awin1.com/cread.php?awinmid='.$params[0].'&awinaffid='.$this->partnerIds[$key].'&ued='.urlencode($url);
break;
Expand Down
9 changes: 5 additions & 4 deletions tests/php/unit/Service/UrlTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public function testUrlExtraction()

public function testAmazonUrlExtraction()
{
$_ENV['PARTNER_AMAZON'] = 'abc-123';
$_ENV['PARTNER_AMAZON_COM'] = 'abc-123-com';
$_ENV['PARTNER_AMAZON_COM_BE'] = 'abc-123-be';
$urlTransformer = new UrlTransformerService();

$url = $urlTransformer->transformUrl('https://www.amazon.com/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3');
$this->assertEquals('https://www.amazon.com/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3?tag=abc-123', $url);
$this->assertEquals('https://www.amazon.com/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3?tag=abc-123-com', $url);

$url = $urlTransformer->transformUrl('https://www.amazon.com/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3?crid=123456789');
$this->assertEquals('https://www.amazon.com/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3?crid=123456789&tag=abc-123', $url);
$url = $urlTransformer->transformUrl('https://www.amazon.com.be/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3');
$this->assertEquals('https://www.amazon.com.be/Zmart-Funny-Christmas-Coworkers-Secret/dp/B0CC1S12S3?tag=abc-123-be', $url);
}

public function testurlReplacement()
Expand Down

0 comments on commit 76b0381

Please sign in to comment.