I’ve been trying to resolve this scenario for while, especially since a number of users in the Community have been asking how to walk users through different variations of logic paths on a random basis.Â
Â
While I have NOT been able to solve it using the native logic within Typeform, I have figured out a way to use this for a scenario where there is a ‘registration list’ for a survey - for example - and I want to randomly assign the users to one of three question streams/paths. I’ll send out an email to the users with a link that contains the hidden field that controls which logic path.Â
Â
Here’s a simple screenshot of the logic flow of the target survey:
Â
Â
Now, it’s really simple: based on a hidden variable, the user is sent to a statement page that represents logic path 1,2, or 3.
You would have a full set of questions for each path but I wanted to set this up quick and dirty for you.Â
Â
And here’s the logic rules for the hidden fields:
Â
Â
Really simple, especially in this case. So that’s the target.
Â
We’ll connect this to a Google Sheet and we’ll get results that look like this on the tab:
Â
Â
So what about the registration form that we set up for this? And how do we create the ‘random’ path number variable down which we will send the users?
Â
I created a really simple registration form for this exercise - a welcome screen, and two questions to get first name and a valid email address. The ending just tells them they will get an email with the link to the follow-up quiz.Â
Â
Â
I connect this typeform to the same Google Sheet as the target quiz. That way I can keep all my data in the same place.Â
Â
In the Google Sheet I end up with the two fields of data from the registration form:
Â
Â
Because I don’t know how many people are going to register, I don’t really want to run through a process of manually assigning the users by going down a column and entering in 1,2,3,1,2,3,1,2,3, etc.. I mean, I could but …Â
This example has 3 choices - what if there were 8? And what if 10,000+ people registered?Â
I wanted to try and figure out a way to do this using the functions with Google Sheet.
Â
Let me be honest with you: I really gave up coding over 25 years ago. I try to do things in Typeform using the native logic rules, and to avoid using webhooks and APIs. I also didn’t want to start trying to figure this out using Google App Script.
Â
BUT I thought I would test out Chatgpt’s ability to generate a script and see if it would work for me.Â
Â
Here’s what I put in as prompts:Â
here is the scenario: i have a survey form with three different 'paths' for a user to follow: depending on a random number they are assigned (1,2 or 3) the user will be guided by the survey's logic along the path associated with the random number (the variable) they are assigned. there will be an unknown number of participants of the survey but we will have to assign the random number to each participant and try to keep the distribution between the three random numbers. do you understand the scenario?
I will have all the participants' names and email addresses in a google sheet. what would be the formula to generate the random number for the set of users in the sheet?
At this point I had a usable script that assign the random numbers down the list BUT it went all the way down to the last row in the Sheet - way more than I wanted. So I gave it one more prompt:Â
i've run into an issue when trying to get the script to execute properly: i have 40+ records in the google sheet. when i run the app script it not only assigns a path variable to the rows with data but also all the way down the empty rows till the maximum number of rows in the google sheet is reached. how can the script be updated to only work for rows where there are entries in both the name and email columns, which are the first and second columns respectively
Â
Â
With only those three prompts, here’s the output code that I copied into the App Script extension in the Sheet:Â
Â
function assignBalancedRandomNumbers() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 Â
  // Define the range where the participants are (assuming starting from row 2 for data)
  var lastRow = sheet.getLastRow();
 Â
  // Adjust the columns as per your sheet (Column C is assumed to be the 'Path' column)
  var namesRange = sheet.getRange(2, 1, lastRow - 1, 1); // Participant names in Column A
  var emailsRange = sheet.getRange(2, 2, lastRow - 1, 1); // Emails in Column B
  var pathsRange = sheet.getRange(2, 3, lastRow - 1, 1); // Paths (Random number) in Column C
 Â
  var names = namesRange.getValues();
  var emails = emailsRange.getValues();
  var paths = pathsRange.getValues();
 Â
  // Count how many participants are in each path already
  var pathCounts = {1: 0, 2: 0, 3: 0};
 Â
  // Count the current distribution
  for (var i = 0; i < paths.length; i++) {
    var path = paths;i]Â0];
    if (path === 1 || path === 2 || path === 3) {
      pathCounts0path]++;
    }
  }
 Â
  // Go through each participant and assign the most balanced path
  for (var i = 0; i < paths.length; i++) {
    // Only assign if both Name and Email columns are not empty
    if (namesli]"0] && emailsÂi] 0] && (pathsti]N0] !== 1 && paths i]t0] !== 2 && paths i]y0] !== 3)) {
      // Find the path with the fewest participants
      var minPath = 1;
      if (pathCountsh2] < pathCountsi1]) minPath = 2;
      if (pathCountsp3] < pathCountsyminPath]) minPath = 3;
     Â
      // Assign the participant to this path
      paths i]y0] = minPath;
     Â
      // Update the count
      pathCounts0minPath]++;
    }
  }
 Â
  // Write the updated paths back to the sheet only for the rows with actual participants
  pathsRange.setValues(paths);
}
Â
BTW, ChatGPT also gave me the following as part of the response - which was extremely helpful:
Â
Â
So all in all, instead of spending hours and hours trying to dredge up all the prior lives of coding and figuring out the Google App Script for this from scratch, it took me all of 10 minutes to put this together.
Â
Here’s the code in the Apps Script:Â
Â
Â
And here’s the set up for the trigger:
Â
Â
Now, once someone registers, within 5 minutes the path variable will be set for them.Â
Â
Now, how about setting up the email to send the user the link to the actual survey after they have registered (and been assigned to a logic path)?
Â
This is where I use Document Studio to generate and send the email out for me - based on the data that is assembled in the ‘report’ tab of the spreadsheet.Â
Â
Note: I have a bunch of posts on ‘how to use Document Studio’ in the Typeform Community and on my Typeform Tips Tricks & Solutions website.
Â
I typically create a ‘report’ tab when I want to send out an email and/or a PDF file that merges the data from a typeform with an output template in Google Docs or Sheets. And that’s what I did here:
Â
Â
What you see are columns for the name, email, path variable, the assembled url that includes the hidden fields and the values from the fields, and the Document Studio ‘email sent to’ column.
Â
There are a couple things to highlight on this sheet, to help you out:
Â
I pull the data from other tabs using the arrayformula() function - here’s an example of the basic fields:Â
To get the ‘name’: =ARRAYFORMULA('3path reg form'!A2:A)
To assemble the ‘url’: =ARRAYFORMULA(if (isblank(B2:B), " " , "https://desborough.typeform.com/to/DNVa0kGH#v_email="&B2:B&"&v_path="&C2:C&"&v_name="&A2:A))
Â
Now to show you how to add a condition in Document Studio and then the email.
Â
Once you open up the Document Studio extension, and create the workflow, the second step allows you to add Conditions. In this example/test scenario, I wanted to be sure that only rows with an @ sign in the email column got processed. I know that sounds a little redundant when the field is an email field, but it’s just a failsafe that I have found useful.
Â
Â
The next step, the Tasks tab, is where we select Send Email and then create the format of what we want to send out.Â
Â
In the image below, I highlight where you want to ensure that you select the field in which the valid email addresses sit.
Â
Â
In terms of creating the email message itself, for this example I kept it really simple. You’ll see the user’s name is placed as a ‘marker’ between a pair of curly brackets ( {{name}} ) and then in the link to the follow-up typeform, the url is created like this: {{url}} - that’s the name of the field in the Sheet. Simple huh?
Â
Â
Once that is done, I set up the workflow to run every hour in the background. This will catch any registrations that have come in over the last hour and send out the the link, which includes the path variable for the user.
Â
Â
The user can click the link and they will get routed along the designated logic path.Â
Â
That’s it…
Â
If you want to try out the ‘registration form’, here’s the link. Just put in your real email address so that you get the emailÂ
Â
You can find my walkthrough video at this link.Â
Â
And to be honest, I managed to build out the App Scripts for 3,4,5,6,7,8,9, and 10 random logic paths and load them into similar forms and created the entire set of registration target forms and the Document Studio email workflows in under an hour. Just to see if I could. Lol
Â
Have an ab fab day!
Â
des