Skip to content

Instantly share code, notes, and snippets.

@annafergusson
Last active March 25, 2024 00:45
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save annafergusson/40195a2fd2703d3a8a85bad61a6ccfe9 to your computer and use it in GitHub Desktop.
Save annafergusson/40195a2fd2703d3a8a85bad61a6ccfe9 to your computer and use it in GitHub Desktop.
Random redirect using Google Apps Script
  1. Under Google Drive, create a new Google Apps Script (you might have to look under "More" after you click the "New" button).
  2. Give your project a name e.g. Random redirect
  3. In the Code.gs tab, delete what is there and replace with this code.
  4. Replace the links to the Google forms with your own :-)
  5. Save the project then select "Deploy as web app" from the "Publish" menu. Under "Who has access to the app" select "Anyone, even anonymous".

You should then get a "Current web app URL:" that looks something like this: https://script.google.com/macros/s/AKfycbwu_Fv8gGGbzMCqot1e1-u8TereYJ9uJVfIMw02NxNUTj9WNII/exec

You can then give people this URL (or use a URL shortener first to make it look nicer e.g. bit.ly, tiny.cc or embed it in an existing webpage), and it will randomly redirect them to one of the Google forms you've added to the code.

function doGet() {
var webpages = [];
//---------------just change the webpage URLs
//the webpages need to hosted on https sites - if you want more than two just create a new line using the same code as below
//just change the link to the google form
webpages.push("https://docs.google.com/forms/d/e/1FAIpQLSfLfuayN7YVhFRzAWG87ArEeML6ltEUgDOB5rA9F0KIH2u_5w/viewform?usp=sf_link");
webpages.push("https://docs.google.com/forms/d/e/1FAIpQLSdjjtjCVrovMpGDH-9JlBwmXI5kNgK_EmJM1sfBRFDw3C4wWg/viewform?usp=sf_link");
//------------------------------------------
var random_page = Math.floor(Math.random()*webpages.length);
var redirect = webpages[random_page];
return HtmlService.createHtmlOutput("<script>location.href = '" + redirect + "';</script>");
//line below requires authorisation but also works - thanks Nikolai Cook
//return HtmlService.createHtmlOutput(UrlFetchApp.fetch(redirect));
}
@annafergusson
Copy link
Author

@annafergusson
Copy link
Author

An updated version of the code using iframes is available here: https://gist.github.com/annafergusson/f3c53a7e48b4c6d2ca6fb29320af2533

@emoore7784
Copy link

Do you have an idea whether, if I randomize four different links, this will equally distribute them over time? Or will it completely randomize? Is it possible to do something similar as a round robin?

@annafergusson
Copy link
Author

It will be completely randomized. You also need to factor in that not all participants will go on to complete the survey even if they get the link, although this could be balanced across your different versions. You would need to keep track of what version you have already sent someone if you wanted to ensure balance :-)

@emoore7784
Copy link

emoore7784 commented Feb 11, 2021 via email

@abhishekn94
Copy link

Hey so I know nothing about any of this but what I need is to be able to share a link which could randomly redirect to one of multiple youtube videos. I tried replacing the google form URL with the youtube URL in the code mentioned above but it didn't work.
Please help!

@annafergusson
Copy link
Author

I don't have capacity to help with individual coding issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment