How I Built an Automatic Affiliate Linking Tool with ChatGPT – No Code

I used ChatGPT to build a tool that automatically turns certain keywords into hyperlinks with my affiliate links.

One thing we do here at AI Growth Guys is affiliate marketing.  That means we put our affiliate link to certain products into our articles.

I love writing articles, but I hate wasting time doing this kind of linking.

If I do this manually, I need to:

This process takes quite a bit of time.  

I wanted to get a program to do this for me.  

I’m not a programmer.  I noticed there are some programs that do this, but a lot of them cost $49 bucks a month, which I don’t want to pay.

So I wanted to see if ChatGPT could do this for me.

Google Apps Script Video – AI Affiliate Link Tool

Let me first explain what I do and what exactly I needed done.

I write my first draft of every article (including this article) in Google Docs.  I find it the easiest place to write in.  Then Addison, my business partner here at AI Growth Guys, cuts and pastes my article into our WordPress editor and does the rest of the formatting and editing.

I wanted this automatic linking software to work directly with Google Docs.

I asked ChatGPT how to do this, and they recommended I use a “Google Apps Script”.

I’d never heard of that, but it’s pretty cool.  This “apps script” also works with other Google products like Google Sheets.

To do this, from inside a new Google Doc, you need to click on:

Extensions > Apps Script

This will open up a new window.  This window corresponds with this particular Google Doc that you are working on.

It looks like this:

I then explained to chatGPT what I wanted to do.

  1. I uploaded all of my affiliate links to ChatGPT with the corresponding keywords I wanted to link from.

(For example:  My affiliate link to Beehiiv is:  https://www.beehiiv.com?via=andrew-best 

I would say, “Beehiiv: https://www.beehiiv.com?via=andrew-best”)

So every time I write the word “Beehiiv”, I want it linked like this Beehiiv

  1. I did that for each of my affiliate links.  I told ChatGPT that everytime I the word “Beehiiv” shows up in my article, I want it hyperlinked with my affiliate link”
  1. I did this with all of my affiliate links.
  1. I said to ChatGPT.  “Make me a Google apps script that does this automatically”.  I also asked, “What is the best way to do this?”

ChatGPT gave the great idea to first make a new menu tab called, “My Custom Scripts”

It looks like this.

A standard Google Doc doesn’t have this “My Custom Scripts” menu option. 

I didn’t know that you could make your own!

Anyway, I asked ChatGPT to first make me that menu bar.

It gave this code:

function onOpen() {

  var ui = DocumentApp.getUi();

  ui.createMenu('My Custom Scripts')

    .addItem('Insert Affiliate Links', 'insertAffiliateLinks')

    .addToUi();

}

In order to implement this, you need to go into your Apps Script, like I showed above by clicking on:

Extensions > Apps Script

Then you delete everything that is in there, which is this:

function myFunction() {

}

So just delete that, and then cut and paste the above code into its place and click “save”.  

Then click “run”.  

You will see a couple of pop up windows that ask for permissions.  These are really simple.  

Just make sure that you are logged in, and click “yes” to allow the permissions they are asking.

At this point, you will have your new menu option called “Custom Apps Script”.

Then I asked ChatGPT to make the rest of the apps script that would automatically hyperlink each of my chosen keywords with the corresponding affiliate link.

Here was the exact script it made for me:

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('My Custom Scripts')
    .addItem('Insert Affiliate Links', 'insertAffiliateLinks')
    .addToUi();
}


function insertAffiliateLinks() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
 
  // Define your keywords and corresponding affiliate links
  var links = {
    '11 Labs': 'https://elevenlabs.io/?from=partnermeyer5644',
    'HeyGen': 'https://heygen.com/?sid=rewardful&via=andrew-best',
    'Mangools': 'https://mangools.com#a57df4ec7285f734f44889a97',
    'TubeBuddy': 'https://www.tubebuddy.com/pricing?a=andrewbest',
    'Skool': 'https://www.skool.com/refer?ref=97d294e27b5b4519847e464d4c6eb947',
    'Beehiiv': 'https://www.beehiiv.com?via=andrew-best',
    'Stammer.ai': 'https://stammer.ai/?via=andrew',
    'Exploding Topics': 'https://explodingtopics.com/?via=andrew-best',
    'Apollo': 'https://apollo.partnerlinks.io/6upajoxr847f',
    'Riverside': 'https://www.riverside.fm?via=andrew-best',
    'Repurpose.io': 'https://repurpose.io/?aff=256322',
    'vidIQ.com': 'https://vidiq.com/aigrowthguys',
    'ConvertKit': 'https://convertkit.com/?lmref=DSxnZA',
    'Go High Level': 'https://www.gohighlevel.com/?fp_ref=hcbyk',
    'Partnerstack': 'https://buy.partnerstackprm.com/927y2duoum3i',
    'CreativeFuel.ai': 'https://creativefuel.ai?via=andrew',
    'Rewardful': 'https://www.getrewardful.com/?via=andrew-best',
    'Tapfiliate': 'https://tapfiliate.com/?ref=mjk2ndr'
  };


  // Iterate over each word-URL pair
  Object.keys(links).forEach(function(word) {
    var url = links[word];
    var foundElement = body.findText(word);


    while (foundElement != null) {
      var foundText = foundElement.getElement().asText();


      // Get the start and end indices of the word
      var startOffset = foundElement.getStartOffset();
      var endOffset = foundElement.getEndOffsetInclusive();


      // Set the link URL for the word
      foundText.setLinkUrl(startOffset, endOffset, url);


      // Find the next occurrence of the word
      foundElement = body.findText(word, foundElement);
    }
  });
}

Basically, if you cut and paste this exact code into the apps script part I showed above, this will work for you.

Note: Obviously, you’d need to make it with your own affiliate links, but that’s the only thing you’d need to change.

I’ll give you 2 screenshots to show you how this works.

Let me first write 2 sentences.  (This will work for a long article, I’m just showing you what it looks like).

I want the following keywords hyperlinked:

Here is how my 2 sentence article looks before I run the script.

You can see that nothing is linked yet.  

I moused over “My Custom Scripts” so you can see the “Insert Affiliate Links” show up in the dropdown menu

I then click on “Insert Affiliate Links” 

And that’s it!!

Here is what it looks like.  I’ve moused over the word “Beehiiv” just to show you that all of these words are actually hyperlinked, and just highlighted in blue.

Now, everytime I write an article, I use this same Apps Script, and then I write the article.  When I’m finished, I click “run” to get all of my links in there.

I’d say this saves me about 15 minutes per article, because I don’t need to find all of my links, and then manually start putting them in.

Another benefit of this time saving thing is that this tool never makes a mistake.

With this extra time, I can immediately start working on my next article and earn more money because of my increased productivity.

Some of the links in this post may be affiliate links. We might earn a small commission at no extra cost to you. It’s one way to support us and keep things running. We also only put affiliate links we believe in fyi.

Author Andrew Best

FREE AI COURSES AND UDEMY DISCOUNTS

Get free courses on AI, Email Marketing, Growth, and Monetization strategies to grow your business and improve Your AI skills.

JOIN US

Related