Skip to main content

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:

 

AD_4nXeF259keJPJPjUbMtPhhoh4KqstOECbzoWhaRws1lTB70EJFnj0HiTONJQHmgizHTzsZFcr_OAfw_cdVHka4fyp2YHRBxhoJxstJgtbIF2JGeLQw1KpVjEpvOTPn6IDNUSBE2HdhXKWTICceaAtuZjS8JY?key=-aBHZXsGchbgrVIIsB1w0is1

 

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:

 

AD_4nXeJo_hI590xCSrbD5nDdgdu1_I571X4AGDXrB1zH3YtbXfRXXiPP3uxTPNGBIJqepY-dt1ZHwzFaaYPOjhSRvr8QHWuod-IvTysiZtcQihfHySqot8iXTZoQV_T3hPe7UZHwCTlOLFlyzoeogESCXPkhgY?key=-aBHZXsGchbgrVIIsB1w0is1

AD_4nXfx_NLCg1gnBojcsu7Bjv1bJ0SOlDmnHW0BDI37y8hzUOsxP-sPNx95TFRCr8LQ1XiYrvY-4JD6YsOltkXGnRl9TfRE_Qytj68lNPlmouiqQBR_-PgTGuzaAhDwKhJV8JvZZX4mJYRIoRCSopZubsT6ZGE?key=-aBHZXsGchbgrVIIsB1w0is1

 

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:

 

AD_4nXdw8WLVMrJHAzaMlIdO2uSch_vIWurFjDs3MrZ4dZ1Xxzlj60cCdRRjLpG7i8VDnM7Pi-RAbB5Vt9f53G_J8NARVS2MjHg-gDxnrGI6GpqPz3NOpslhawih60ERB8Ui4ef0s6jQS9i1wRV_Xv2FcXtwtCk?key=-aBHZXsGchbgrVIIsB1w0is1

 

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. 

 

AD_4nXfsm3L1dbZ9OcJ2hKWRhXPMS3HuSsjwbGAsAF0K6fGObx7N2LMMKPrM_Wlh8f5AysVS8NEE98j-2VVQ7Omea6awOT35RUqvdJ-hIUWI1kKtnYAX6aNTSkqtcsMK_wHWbooqurLQKwAmuXEfsp8z608eoeY?key=-aBHZXsGchbgrVIIsB1w0is1

 

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:

 

AD_4nXeCM9UXaXCWZo-TdHxn4Ih_VxZu9TPpcMlBzaz41KdddSsZ88LXCTFx9gWOQi4CEXzjVJtH4kr05jSs_KkwS7ILsElfWEd8hWlTiDNSkGhuqu6lwT9_gQAoo2nfMV3vQOGSHo1GGi1LGVojJbwE_dQTjd8?key=-aBHZXsGchbgrVIIsB1w0is1

 

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:

 

AD_4nXcf0kasB8zmaQlSL9ukm_VPzQ7BFz2wmeDGFUZNcMU8FqzZ_zROIXYQ4dVNO3hNbWy5R7n9aku7Ddcw5WaC1Tru0MRLYwQdHMdORPtX6dnvPMmkjA92EiHt8mseMALF9Cah-PqW-vRFZO5_JS7gOvnqFg?key=-aBHZXsGchbgrVIIsB1w0is1

AD_4nXfQKoD5iU3Tu7tVxmJhHA9_BJHSjpNNjUV3Y-dDO0xlHTC7V3Jxkw7KhRs_K8TdGyfvDnppc0ntx2YYUHDOkxrm66iMhGepgU-S8eYMCtLXNoKWogmj-uyINe8J8Z_gbqViFs_pH-oXg701x7f4vQOs_l8?key=-aBHZXsGchbgrVIIsB1w0is1

 

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: 

 

AD_4nXfE-AhkaweeLTqEipOx1_v5ROn6nAoX-Yow3IjCVplndGg4-zQVQFvuTGtu4AdTrW786jo9NpDjl3d8vvQOvHrWerUnQrcJ0pfVj4UP0S2Cr4BS1BKN7k_DxikWodpxNpZSmOJk5mYZ4QTlapbJsEEIPw?key=-aBHZXsGchbgrVIIsB1w0is1

 

And here’s the set up for the trigger:

 

AD_4nXfRJcf2iMe-VA5HK70QRVe10TcPnOMbhvNnt7W-l3mkyr046K7qeKMTMJ8wLnem9Fnl50kHgk0Wu9FYIlhftaODrRxaY387b66mZVz500PKeskm_-B5MOIF6M9BUIQFRa77M6faKWN7H2DrQVIsE2e3xgI?key=-aBHZXsGchbgrVIIsB1w0is1

 

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:

 

AD_4nXdemIrvB9q13DJwNBkkuxzy1qltDM4vDzXVkxwJsXp4htRN9c2aVH51AM_5nA0JJy33cW9QyX8sHaWTXLvIgq-XC6qtz22nybXZgEUXfIEOONHdHipXhmhJI5JrHoGSxuA0ZdQjaTq4ItxTqGC8l1YZew?key=-aBHZXsGchbgrVIIsB1w0is1

 

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.

 

AD_4nXdJpdmE4NGsGsqJKE24_Gth90sAt2NStEOy_CwSZMd08jwejM8e4gGENsarDDfbEeqhKV4vRnR3_Fy6qtDZIF58S_O9aG0b58uniW9P644r9E0lolJnANMhAxeJnhy7FagRquuqg77yjaSJXTxcBn9sF74?key=-aBHZXsGchbgrVIIsB1w0is1

 

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.

 

AD_4nXfkspv2Pcjb8mZkrcQSl020jnn2Vv9IlCcfLrCmsfzGpuHUEl3F49UrN1xeamGhBK4AK2un1_gKORrWBtTR11ChsjXE2s67XwtxccL58yPxQD2rMYbHXcE28iyKJ3UgMtBQZg6344uC0baKAyLfaLI9FA?key=-aBHZXsGchbgrVIIsB1w0is1

 

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?

 

AD_4nXf92885VdqLIe1X9Ygk10gTHid1QFpK7BqXzShY7amXnZ0vJzg3ZorRPHODMXduYlZ-9ctAVbxUN3EI-Nvq7R3hS2AxPCHNfMhhpEaqCks1BXi2NjJibqRrkdOKS3wg_fENi2g1R6OOBRBBWUUfkzQc5w?key=-aBHZXsGchbgrVIIsB1w0is1

 

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.

 

AD_4nXdb_2FB6yDNfsNwzkuaNFylBUdrWQSnGTPupSRPmFIoh6sf5J7etQlNWWoDb60Xd83ZMH1H93ViiJdIGBpIkE5lQ9mwl9L9-fBn5dgkq-ntBkVQlOAgw00xZ5McYcWrsYiIJS18r9QrNIzBNFOckDzdOgI?key=-aBHZXsGchbgrVIIsB1w0is1

 

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

Be the first to reply!

Reply