Last night I posted a TextExpander snippet that automated the procedure for generating Apple affiliate links for the items in iTunes and Mac App Stores. This morning I got an email from David Smith, who pointed out that affiliate links can be much shorter and easier to read if you’re willing to forgo the click tracking that LinkShare does. He linked to his own post from last week that describes the process very well. I liked David’s way of making affiliate links so much I’ve dropped last night’s snippet and made a new one.

The one advantage of the previous snippet is that it allows you to use LinkShare to track clicks as well as purchases. With the new snippet, you can track purchases only. To me, that’s a small price to pay to go from frightening looking URLs like this

http://click.linksynergy.com/fs-bin/stat?id=L4JhWyGwYTM
&offerid=146261&type=3&subid=0&tmpid=1826
&RD_PARM1=http%253A%252F%252Fitunes.apple.com%252Fus
%252Fapp%252Fnotesy-for-dropbox%252Fid386095500
%253Fmt%253D8%2526uo%253D4%2526partnerId%253D30


to nice, easy-to-read ones like this

http://itunes.apple.com/us/app/notesy-for-dropbox/
id386095500?mt=8&partnerId=30&siteID=L4JhWyGwYTM


(In both cases, I’ve inserted line breaks so you can see the whole thing without horizontal scrolling.)

As before, the links are generated through a Python script:

python:
1:  #!/usr/bin/python
2:
3:  from subprocess import check_output
4:  from sys import stdout
5:
6:  # The procedure followed here is taken from Apple's instructions for for linking:
8:
9:  # My affiliate ID.
10:  myID = 'useyourown'
11:
12:  # Get the URL from the clipboard.
13:  clipURL = check_output('pbpaste')
14:
15:  # Add my ID and the partnerId parameter to the URL. If there are already
17:  if '?' in clipURL:
18:    itemURL = '%s&partnerId=30&siteID=%s' % (clipURL, myID)
19:  else:
20:    itemURL = '%s?partnerId=30&siteID=%s' % (clipURL, myID)
21:
22:  # Write it out
23:  stdout.write(itemURL)


which you can copy and paste into a new TextExpander shell snippet:

The script is so much simpler than the LinkShare-ified version I wrote before, I’m almost embarrassed to post it. The only real logic is in Lines 17-20, which decide whether the partnerID parameter should be preceded by a & or a ?.

The one thing you’ll have to do if you want to use this snippet yourself is change Line 10 to put your own affiliate ID between the single quotation marks. You can pull your ID out of any affiliate link you might have generated in the past through Apple’s Link Maker tool. Look for a portion of the URL near the beginning that looks like

…/stat?id=L4JhWyGwYTM&offerid…


Your ID is the alphanumeric string between the = and the &.

You can, of course, use whatever abbreviation you like for the snippet; I settled on ;aal for “Apple affiliate link.”

The procedure for using this snippet is basically the same as the one I gave last night:

1. Find the item you want to link to in iTunes, the Mac App Store, or in your browser on one of Apple’s product web pages.

2. Copy the link to the item from either the popup menu (in iTunes or the App Store) or the toolbar (in your browser).

3. Switch to wherever you want to put the link and type ;aal to insert the URL.

As I said in last night’s post, I’d like to cut out Step 2 but can’t figure out how without resorting to GUI scripting, which I’m always leery of.

With this snippet, you can make affiliate links to

Update 1/7/12
If you’re using Snow Leopard or earlier, you probably have a version of Python earlier than Python 2.7, which means your subprocess library won’t have the check_output convenience function. You’ll need to use this version of the script instead:

python:
1:  #!/usr/bin/python
2:
3:  from subprocess import *
4:  from sys import stdout
5:
6:  # The procedure followed here is taken from Apple's instructions for for linking:
8:
9:  # My affiliate ID.
10:  myID = 'useyourown'
11:
12:  # Get the URL from the clipboard.
13:  clipURL = clipURL = Popen('pbpaste', stdout=PIPE).communicate()[0]
14:
15:  # Add my ID and the partnerId parameter to the URL. If there are already
17:  if '?' in clipURL:
18:    itemURL = '%s&partnerId=30&siteID=%s' % (clipURL, myID)
19:  else:
20:    itemURL = '%s?partnerId=30&siteID=%s' % (clipURL, myID)
21:
22:  # Write it out
23:  stdout.write(itemURL)


The difference are in Lines 3 and 13.

This script will work under Python 2.7, too, but Line 13 is distinctly more opaque.

Frankly, the whole subprocess module—and the popen2 module that preceded it as the Pythonic way of shelling out—stinks on ice. Anyone who wants to run shell commands from within a script already knows the shell syntax; forcing them to use an unnatural syntax, no matter how Pythonic it may be, is wasteful and stupid. Perl and Ruby are much smarter about this. I agree with this guy and will be checking out his Envoy library.

## 8 Responses to “Simpler Apple affiliate linking”

1. jihuhsdh says:

“Your request produced an error. [newNullResponse]”

From this url:

http://itunes.apple.com/us/app/notesy-for-dropbox/ id386095500?mt=8&partnerId=30&siteID=L4JhWyGwYTM

2. jihuhsdh says:

maybe the line break is causing problems with Firefox’s “open link feature”

3. jihuhsdh says:

Thats it

4. And to think that I’ve been defending my decision to allow comments here.

5. Jim Parr says:

Why not use one of the free URL shortening services such as TinyURL to create a really short one? TinyURL now allows you to pick your own abbreviation as long as you use only letters, digits and dashes and the abbreviation you’ve chosen isn’t in use. That way you could keep the advantages of your super long URL, but have a small one to link to it.

6. I could do that, Jim, and it would be interesting to program the “unique but readable” part of the URL, but URL shorteners just add one more level of redirection, something I’d like to avoid.

Also, I think people have more confidence when they roll over a link and see http://itunes.apple.com appear in their status bar. They know exactly where the link will take them.

But if you take my older snippet and wedge in a call to TinyURL’s API after the double percent encoding, I’d like to see it. Maybe I’d change my mind.

7. Ronn says:

This is way over my head, which I why I probably can’t seem to get it to work.

Other than following the directions above, is there anything else that I would to do to TextExpander to get this working?

Thanks!

8. Ronn, email me (my address is in the sidebar) a description of what happens when you use it, and I’ll try to give you a hand.

My first guess (because I make this mistake all the time): If TextExpander prints out the Python source code instead of a link, you forgot to set the snippet content to Shell Script. Click the popup above the content area in TextExpander to change it.