Aweber is a great email marketing service that is very popular because of the advanced features that it allows.
One great feature is the fact that Aweber allows you to use custom variables, but many people don’t know or don’t use this feature. You should though, because this allows you to a lot of cool things. In this post, I’ll show you how you can automatically track who referred a subscriber to you, and insert their affiliate link in your emails. This is good to have for merchants, because you can run an email list on your sales page, and still get affiliates to wholeheartedly refer visitors to you still.
Requirements:
- PHP
- Aweber
To do this, you’ll need to setup a custom variable in Aweber. Let’s call it “ref”. Here’s a screenshot of where to do this in Aweber.

Now that you have a custom variable, we need to use it in our opt-in forms. The problem here though, is that the form builder in Aweber doesn’t allow you to insert PHP code (which is what we require for this to work).
So just create your opt-in form as per normal, but without the ref field (you’ll see it in the form builder). Now this is where things differ: when it asks you to publish as Javascript or HTML, choose HTML. Grab that code, stick it on your website where you want it, and paste this line of code right before the </form> tag. Remember to change the words YourDefaultRefGoesHere. This will set a default ref to use, just in case the visitor got to your form without a referrer (Thanks Mike!).
Alert: The only drawback for this method is unlike the Javascript method, you won’t be able to track impressions on your form. The benefit is that EVERYONE can view a HTML form, whereas some people may turn off Javascript, or have a browser that does not support Javascript, so they won’t be able to see your Javascript form.
Now that the form and variable is complete, we’ll need to actually use it. The following screenshot shows you how to do so. Simply create a broadcast or followup message as per normal, but click on the “Personalize” link, and choose your custom variable, as shown.

You’ll notice 2 variants of your variable listed, and here’s what they mean. For our case, we can just use the first version, which is {!custom ref}.
{!custom variable_name}: Whatever information is stored in the variable_name custom field
{!custom_fix variable_name}: Whatever information is stored in the variable_name custom field, with the first letter of each word capitalized
After this, anyone who gets referred to your page like so: YourPage.php?ref=jack will automatically have the “jack” put into the custom variable of “ref”, and anytime you want to dynamically insert the referring affiliate, just use {!custom ref} in your emails.
An example link will be:
http://www.yourdomain.com/product.php?ref={!custom ref}
Viola. Now you can dynamically insert affiliate links in your emails or track referrals with Aweber
If you don’t already have Aweber, I highly recommend it because of the features and performance that it offers. The above technique is just one small feature that Aweber offers – think about Geo-targeting, or split-testing…just some more of the advanced features that Aweber offers.
Update: This works if you’re a Clickbank affiliate as well. Just change “ref” to “hop”, and you can use the following URL structure:
http://{!custom hop}.MERCHANT.hop.clickbank.net
Now your affiliates who refer visitors to your email list can continue earning their rightful commissions!
Similar Posts:
- How To Integrate Facebook With AWeber Sign-up Form
- HTML Button That’s A Link
- PostAffiliate Pro vs iDevAffiliate Review – Which to Affiliate Tracking Script Choose?
- Use Javascript to Display Random Image and Link
- GMail Trick


August 11th, 2010 at 5:58 pm
you are the shit..thanks alot for the awesome info….
August 11th, 2010 at 6:54 pm
No problems!
November 6th, 2010 at 3:42 pm
Bro! whats up man? Long time. I'm so glad I found your blog while looking for a solution to my desired dynamic aweber setup
Let's catch up, get me on FB http://www.thatcher.tv
March 4th, 2011 at 10:34 pm
Hi, this is an awesome post! I was trying to figure out how to do this for Clickbank hop codes and Aweber for ages.
I’ve stumbled upon a problem with your code that I’m just not knowledgeable enough to fix right now though. If there is no hoplink, then the value recorded by Aweber is null, so when you email out your hoplinks via Aweber some of then will be http://.MERCHANT.hop.clickbank.net which is an invalid URL.
Is there a way to set a default hoplink (like NONE) in the PHP instead of null?
Thanks, Mike.
March 5th, 2011 at 1:10 am
ah, good catch. there is a way actually, and i’ve updated the code with the changes (the form code that you have to put on your page). this will allow you to default to a certain hoplink when the referrer is null. Just look for the part that says YourDefaultRefGoesHere and change that to the default ref that you want
March 5th, 2011 at 11:19 am
Hi Alvin,
Thanks for the quick response!
Unfortunately, I can’t get your new code to work. At first I thought it was because of the quote symbols in the php code, but even after changing the “s to ‘s, my server just shows a blank page.
Any ideas?
Thanks, Mike.
March 5th, 2011 at 11:41 am
Hey Mike, I think there was a problem with the code. I’ve updated it – it should work now
March 29th, 2011 at 2:55 pm
Thank you! I’ve been searching for a solution for this for AGES.
May 24th, 2011 at 11:09 am
Hi alvin I used the code exactly as you said but when i put in the {!custom ref} into my emails the value that it uses is a load of php code!
This leads me to believe that the code is wrong somewhere.
Do you have any ideas?
Many Thanks
Alex
May 24th, 2011 at 12:04 pm
From what you described, I’d say you’re using the code on a HTML page. Use a PHP file instead to contain your form and you should be all set.
May 25th, 2011 at 5:27 am
Great advice alvin i will try this. But how do I point to the php file from my html page?
Ie what do I put into my html page to activate the php file.
Many Thanks for your response alvin!
Alex
May 26th, 2011 at 12:20 am
A PHP document is a type of file. So instead of contact.html, you’ll have contact.php.
June 26th, 2011 at 4:00 pm
Hi Alvin,
Thanks for the code. Before I use it, does this violate aweber’s terms of service. Here’s what they said: “Submitting subscribers to AWeber via a server-side script (PHP, ASP, ColdFusion, etc.) is not permitted. ”
Since this is using a php code, is this in violation?
Thanks!
-Nick
June 26th, 2011 at 10:21 pm
Plenty of people use server scripts. Even the wordpress blog here is on PHP. The key is submitting subscribers. They don’t want you to submit subscribers without their explicit consent (which you can do with scripts). Tracking referrals is perfectly fine.
July 1st, 2011 at 6:54 am
im struggling from last few days to achieve the same with Aweber and my capture pages but now working, it’s taking the default value only
plz guide , thanks in advance
July 1st, 2011 at 9:39 am
@Devendra What’s the default value?
July 1st, 2011 at 1:10 pm
in Aweber custom field always stored value i replace on “YourDefaultRefGoesHere” but it’s not capturing the actual Affilaite id from the link
July 1st, 2011 at 8:24 pm
Sorry, but I don’t understand
July 4th, 2011 at 2:18 am
Hi, ive applied perfect as per your instruction in Aweber and on my page [ i have replace only single value in script code i.e.=> else { echo ‘admin’; }
Here Affiliate Link is http://www.fireyourboss90days.com?ap_id=psminc
Aweber not inserting Affiliate id i.e. “psminc” but it always capture “admin”
Please Guide to resolve
July 5th, 2011 at 10:17 am
If you’re using my code exactly, then your variable name needs to be ref, not ap_id. e.g. http://www.fireyourboss90days.com?ref=psminc
July 6th, 2011 at 6:43 pm
Alvin,
Can you give us an example of using this code on an actual php page? One where we can view the source code so I can learn how to integrate aweber’s html form into php?
Thanks,Nick
July 7th, 2011 at 1:21 pm
I don’t have one handy now, but a PHP page is just like a HTML page. In fact you can save a HTML page as a PHP page directly and see no difference. E.g. index.html -> index.php
The only difference is that PHP pages allow you to run PHP code. Just put in the aweber HTML form into your page, save the page as a PHP file, and put the code (in the textfile in my post) just before the tag and you’re all set.
July 15th, 2011 at 1:35 am
Dear Alvin
Thanks, Thanks a Lot again… it’s done.
i created “ap_id” in Aweber Variable and it’s working well and capturing the Affiliate code in aweber.
Regards,
Devendra
July 26th, 2011 at 12:34 pm
Does the php code work for dynamic affiliate links that re-direct. For example, if some one clicks this link http://www.kickstartcart.com/app/?Clk=4274744 they get automatically redirected, say to this link: http://www.mysite.com where http://www.mysite.com is displayed in the address bar and the actual affiliate link is masked. Will that code still pull the “4274744″ into the web form?
Thanks,
-Nick
July 26th, 2011 at 1:16 pm
Depends on how you do the redirection, because you can have the variables posted to the new page I think (I’m not that sure about redirections). Best way is to come up with a simple PHP script that just echoes out the code in place of the actual script, and see if that works in your scenario.
September 22nd, 2011 at 1:25 pm
Hi Alvin,
When i’ve sent the “http://www.yourdomain.com/product.php?ref={!custom ref}” to my affiliates, they don’t see anything appearing on the custom ref portion of the url.
Is it because i’ll have to manually add each one of their affiliate id into the aweber database one by one before it appears properly?
Is there any way it can be done in an easier manner? generation of ids automatically when someone registers on the form.
hope i’m making sense
September 22nd, 2011 at 10:31 pm
Did you add the code (in the text file in my post)? That’s the trick that gives you the IDs dynamically.
October 6th, 2011 at 5:07 am
For those affiliate marketers using Aweber, how have you satisfied the postal address requirement? Do you have a PO Box?
October 6th, 2011 at 7:51 am
Definitely a PO box or some other form of mail forwarding address, or your office. Your residential address should never be listed IMHO.