Skip to content

Commit

Permalink
Add support for Amazon short links (#79)
Browse files Browse the repository at this point in the history
Adds support for the following short links and introduces a new setting for a tracking code to use with amzn.eu.

* a.co
* amzn.eu
* amzn.to
  • Loading branch information
overgrow authored Nov 29, 2024
1 parent ae1afd2 commit c38c85f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ en:
affiliate_amazon_cn: "Your amazon.cn (China) Associates ID."
affiliate_amazon_co_jp: "Your amazon.co.jp (Japan) Associates ID."
affiliate_amazon_co_uk: "Your amazon.co.uk (United Kingdom) Associates ID."
affiliate_amazon_com: "Your amazon.com/amzn.com (United States) Associates ID."
affiliate_amazon_com: "Your amazon.com/amzn.com/amzn.to (United States) Associates ID."
affiliate_amazon_com_au: "Your amazon.com.au (Australia) Associates ID."
affiliate_amazon_com_br: "Your amazon.com.br (Brazil) Associates ID."
affiliate_amazon_com_mx: "Your amazon.com.mx (Mexico) Associates ID."
Expand All @@ -15,4 +15,5 @@ en:
affiliate_amazon_in: "Your amazon.in (India) Associates ID."
affiliate_amazon_it: "Your amazon.it (Italy) Associates ID."
affiliate_amazon_nl: "Your amazon.nl (Netherlands) Associates ID."
affiliate_amazon_eu: "Your amzn.eu (Europe) Associates ID."
affiliate_ldlc_com: "Your ldlc.com Affiliate ID (without the #)."
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ discourse_affiliate:
affiliate_amazon_nl:
default: ""
client: true
affiliate_amazon_eu:
default: ""
client: true
affiliate_ldlc_com:
default: ""
client: true
Expand Down
5 changes: 4 additions & 1 deletion lib/affiliate_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ def self.create_amazon_rule(domain)

def self.rules
return @rules if @rules
postfixes = %w[com com.au com.br com.mx ca cn co.jp co.uk de es fr in it nl]
postfixes = %w[com com.au com.br com.mx ca cn co.jp co.uk de es fr in it nl to co eu]

rules = {}

postfixes.map do |postfix|
rule = create_amazon_rule(postfix)

rules["amzn.com"] = rule if postfix == "com"
rules["amzn.to"] = create_amazon_rule("com") if postfix == "to"
rules["amzn.eu"] = rule if postfix == "eu"
rules["a.co"] = create_amazon_rule("com") if postfix == "co"
rules["www.amazon.#{postfix}"] = rule
rules["smile.amazon.#{postfix}"] = rule
rules["amazon.#{postfix}"] = rule
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/affiliate_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def r(url)
SiteSetting.affiliate_amazon_com = "sams-shop"
SiteSetting.affiliate_amazon_ca = "ca-sams-shop"
SiteSetting.affiliate_amazon_com_au = "au-sams-shop"
SiteSetting.affiliate_amazon_eu = "eu-sams-shop"

expect(r("https://www.amazon.com")).to eq("https://www.amazon.com?tag=sams-shop")
expect(r("http://www.amazon.com/some_product?xyz=1")).to eq(
Expand Down Expand Up @@ -47,6 +48,13 @@ def r(url)
).to eq(
"https://www.amazon.ca/Dragon-Quest-Echoes-Elusive-Age-PlayStation/dp/B07BP3J6RG/ref=br_asw_pdt-5?tag=ca-sams-shop",
)
expect(r("https://amzn.to/d/some_short_link")).to eq(
"https://amzn.to/d/some_short_link?tag=sams-shop",
)
expect(r("https://amzn.eu/d/some_short_link")).to eq(
"https://amzn.eu/d/some_short_link?tag=eu-sams-shop",
)
expect(r("https://a.co/some_short_link")).to eq("https://a.co/some_short_link?tag=sams-shop")

# keep node (BrowseNodeSearch) query parameter
expect(r("https://www.amazon.com/b?ie=UTF8&node=13548845011")).to eq(
Expand Down

0 comments on commit c38c85f

Please sign in to comment.