Google ReCaptcha
This article will show you how to use Google ReCaptcha
Background
The 'Google ReCaptcha' integration will all you to add in a way to confirm each respondent is indeed a real person that will be able to provide valid and meaningful data in your research.
The sections listed below will show you how to program this exercise in your survey.
- Adding Code to the 'Survey Header'
- Adding a Pass/Fail 'Radio Question'
- Adding a 'Static Element' for Display
- Troubleshooting
Adding Code to the 'Survey Header'
- Access the 'Survey Settings'
- See article: Survey Settings Overview
- Click on 'Default Text'
- Click on the link to the 'Full Content Editor' next to the 'Survey Header'
- See article: Full Text Editor
- Click on the source (</>) icon in the text toolbar. You will now be in the source view.
- See article: Source Code Editor
- Copy and paste the following code into the text area
<script src="https://www.google.com/recaptcha/api.js?render=explicit"></script>
- Cli on the source (</>) icon again and to exit the source view.
- Click on the 'SAVE' button at the bottom of the screen
Adding a Pass/Fail 'Radio Question'
- Add a Radio Question
- See article: Standard Question: Radio items (Single Select)
- Name it whatever you need it to be
- Leave the question text blank
- Access the 'Full Text Editor' for the 'Instructional Text' field
- See article: Full Text Editor
- See article: Full Text Editor
- Click on the source (</>) icon in the text toolbar. You will now be in the source view.
- See article: Source Code Editor
- Copy and paste the following code into the text area
<script> var failed = 0;</script>
- Add 2 blank responses to the response list
Adding a 'Static Element' for Display
- Add your 'Static Element' on the same page as your 'Radio Question'
- See article: Standard Question: Static Elements
- Name the question 'CAPTCHA'
- Access the 'Full Text Editor'
- See article: Full Text Editor
- Click on the source (</>) icon in the text toolbar. You will now be in the source view.
- See article: Source Code Editor
- Add your question text surrounded by the div tag as shown below. The code is below as well so you can copy and paste.
<div id="myCaptcha">Please verify yourself using Google ReCaptcha below.</div>
- Click on the source (</>) icon again and to exit the source view
- Click 'SAVE'
- Click on the '+ JavaScript' button
- See article: Adding JavaScript and/or CSS to a Question
- Add the code below making sure to replace every instance of "S3" with whatever you named your radio question
var pass = false; jQuery("#question-S3 ul.cm-radio-response-set li").hide(); var faildetector = jQuery("#question-S3"); var captchaWidgetId = grecaptcha.render('myCaptcha', { 'sitekey': '6Le9jgoUAAAAAF1RRRCEHAecz5druabTbn1gXfvp', 'callback': function(response) { if (response) { pass = true; faildetector.hide(); jQuery("#question-S3 ul.cm-radio-response-set li:eq(0) input").prop('checked', true); jQuery(".error").remove(); } } }); faildetector.hide(); cmJq('.cm-Survey').on('beforeSubmit', someFunc); function someFunc() { faildetector.hide(); if(failed == 0 && pass == false ) { jQuery("#myCaptcha").append("<div class='error'>Please verify yourself using Google ReCaptcha.</div>"); } if (faildetector.val() == "") { failed++; faildetector.hide(); } if (failed >= 2 && pass == false) { jQuery("#question-S3 ul.cm-radio-response-set li:eq(1) input").prop('checked', true); } }
- Click on 'SAVE'
- Click on '+ CSS'
- See article: Adding JavaScript and/or CSS to a Question
- Add the following code
#myCaptcha div:first-child { margin: 2em 0; }
- Click 'SAVE' when you are done
Troubleshooting
- If you want to term if they fail the ReCaptcha, add a logic statement that reads if your Radio Question equals 2, term.
- If you are using this as the first exercise in your survey, you will need to add an intro page before it so the code has time to run. We suggest adding the intro page with a timer to show the next page button after 5-10 seconds.
- See the following article for help with setting that up: Next Page Button: Count Down Timer
- Make sure to test to ensure the exercise is working properly.