Shortcuts Tips: Amazon Affiliate Linker
If you’ve ever tried to copy an Amazon item link, you know that more times than not, it’s horrendously long and filled with tons of unnecessary tracking or reference tags. And if you’re an Amazon Affiliate, you know that it’s not terribly easy to get your unique affiliate link out of the mobile app, especially if you have multiple Amazon accounts (nor does the SiteStripe play nicely with iOS Safari).
And if you’re like me, you’d want one tool to fix both those problems. You’ve come to the right place.
My Amazon Affiliate Linker shortcut ingests Amazon URLs (both long and short versions) and spits back out a nicely formatted
smile.amazon.com link with your Affiliate ID that can be opened, shared, or copied.1 Here it is in action on an iPad:
Get the Amazon Affiliate Linker shortcut → Original Version | Latest Version
A few months back, I wrote a post about my desk setup, and a page to track my current gear. It allowed me to catalog what I use, made me clean up my desk to look presentable, and let me rapid-fire my opinions on my daily-use tech. It also provided the prime2 opportunity to check out affiliate links with Amazon. As a free site, any way that I can recoup some costs associated with keeping HeyDingus online without detracting from the reading experience is worth looking into.
Signing up to be an Amazon Affiliate is easy, and getting the tagged links from their desktop site is pretty straightforward with Amazon’s built-in SiteStripe. There are even tools made for the Mac to clean up and tag links, which I’ve used a few times. But, as I mentioned above, it’s challenging to get the links from Amazon’s mobile app or on iOS in general. That’s where Shortcuts — particularly now that it’s available across iPhone, iPad, and Mac — comes in.
My Shortcuts Philosophy
When I make shortcuts, I try to keep two things in mind:
- Make the actions obvious so they can be understood when it eventually needs to be updated or debugged.
- Use built-in actions, rather than ones provided by third-party apps, whenever possible. Especially if it’s going to be shared, I’d prefer that users not need to download additional apps.
The most straightforward way to match specific parts of a URL would be to use RegEx. But I don’t know RegEx, which violates guideline #1. Or I could use more advanced and user-friendly actions from something like ToolBox Pro to isolate what I needed, but then there’s guideline #2 out the window.
For a little while, I used a somewhat basic version of the shortcut you see below. It went something like this:
- Get URL.
- Expand URL.
- Split the URL by forward slashes, and keep the first four or five segments.
smile.amazon.comand add the affiliate tag to the end.
- Copy to clipboard.
It worked with the most straightforward URLs but utterly failed when it came to more-complicated-but-still-common links. This new version builds in more smarts based on how I’ve learned that Amazon product URLs are structured and gives more options for using the new link.
And, best of all, it follows my two guiding principles. It’s all native actions available on both iOS and macOS. Plus, the actions follow logical sense, even without the documentation comments I’ve added. (By the way, I tend to put those comments before the actions they describe.)
Without further ado, let’s take a look!
How It’s Made
1️⃣ I’ve configured the shortcut to accept text, rich text, Safari webpages, and URLs as input. That allows it to get both regular links that are shared from long-presses on URLs or from Safari, and also from plain text links that I highlight and share from the copy/paste menu.
You might have noticed that it’s set to receive from Nowhere, though. That’s because I consolidate most of my link-related shortcuts behind a separate URL Menu shortcut so as not to overpopulate the Share Sheet.3 So if you want this shortcut to appear in the Share Sheet as its own option, just change Nowhere to Show in Share Sheet.
If no input is found, it falls back to checking the clipboard.
2️⃣ The first Text action is where you’d put your Amazon Affiliate ID. It is populated by answering a setup question but can be changed later by replacing the text in this action. Per Guideline #1, I’ve expressly set the variable here, even though the Magic Variable produced could be named on its own.
3️⃣ A Get URLs action ensures that a proper URL is generated even if text is passed as input. Next, that URL is expanded with Expand URL to take care of any URL shortener or redirect tricks. Finally, the expanded URL is split by forward slashes using Split Textso that each component can be acted upon individually.
4️⃣ A Repeat With Each action paired with an If action checks each component to see if it’s the ‘dp’ or ‘gp’ string that always precedes the Amazon Standard Identification Number or ASIN.4
5️⃣ If it is ‘dp’ or ‘gp,’ a Calculate action adds 1 to the repeat index, which is the component’s number in the list of parts split in Step 3️⃣. By adding 1 and setting a new variable, I can refer to the ASIN with its index number later. Since I can’t be sure where the
dp/ASIN part of the URL will occur in the raw link, this bit of math is my workaround. The repeat action does nothing with any other bits of the URL.
6️⃣ Now, I get the ASIN with a Get Item from List action by using its precise index number.
7️⃣ The only problem with the ASIN part of the URL is that sometimes, that string between forward slashes includes reference trackers of its own. Since I only want the actual ASIN, I had to figure out how to isolate it. It turns out that ASIN are always ten characters, and I’ve only ever seen them be at the front of that string of text. So, with another Split Text action, I break the string apart by each character. I only keep the first ten characters using Get Item from List and put them back together with Combine Text. That result overwrites as the new ASIN variable.
8️⃣ This step is where it all comes together, literally. Using a URL action, I build the new URL with only the bits I need.
https://smile.amazon.com/dp/ is the standard domain. The ASIN variable pulls in those ten characters that refer to a specific item on Amazon. Finally, the Affiliate ID tag is added to the end.[I always feel better about linking to a full URL where you can see the Affiliate ID. It feels more honest than obscuring it with a shortened URL. If I find some time, I plan to update all the short affiliate links on this site to these more readable versions.]
9️⃣ And finally, what to do with the new beautified and affiliated URL? I’ve included opening it in Safari, sharing it with the Share Sheet, and copying it to the clipboard in the Choose from Menu action. Of course, you can always add your preferred destinations to the menu. You’ll notice that the menu shows the final URL, which is an excellent way to confirm that the shortcut manipulated it correctly.
In the example video above, this shortcut changes the URL for a Black Diamond Contact Crampon from the awful
to a human-readable
Turning my original straightforward, 6-action shortcut into a 38-action one doesn’t scream simplicity. But I am proud of how I worked around most edge cases without resorting to something like RegEx. One day I’ll learn what those obscure patterns mean, but that day is not today. I also think and hope it makes the shortcut more approachable to more people. Since I often use others’ shortcuts as starting points for mine, I want people to feel like they follow what’s happening and that they can adjust it to fit their needs.
As for me, I’m glad that I can now use the same lightning fast tool5 across each of my devices.
Get the Amazon Affiliate Linker shortcut → Original Version | Latest Version
By shopping through Amazon Smile, each of your purchases will benefit a charity of your choice. 0.5% of every eligible purchase gets donated straight to them. I also use a Safari extension to make sure every Amazon link gets redirected to a Smile link, so I never have to remember. ↩︎
😆 Sorry about that one. ↩︎
A tip for another time. ↩︎
What are ‘dp’ and ‘gp,’ you ask? No idea. I like to think of them standing for ‘direct product’ and ‘global product,’ but my quick web search did not reveal an actual answer. ↩︎
Text manipulation is quick! ↩︎