Survey results - using API to get count

Options

What is the best way -- or at least what are the options for showing the number of people who have taken a survey?

So far the best to me looks like having the survey add people to a group, then using the API to get the group members, and getting the totals from the header or that.

What I was hoping though was that I could get a number by src code or the like. Any way to do this?

Also (as an alternate) -- is there any way to use a convio thermometer with Surveys? (I think the answer is no, but I want to confirm)

Thanks,

Adrian

Tagged:

Comments

  • having just tested getGroupMembers -- the downside to this is that it has to be server side (or a logged in admin).

    With advocacy where I can get a number with the client. any options for surveys? Without having some server side code elsewhere?

    thanks,

    Adrian

  • Adrian Cotter:

    having just tested getGroupMembers -- the downside to this is that it has to be server side (or a logged in admin).

    With advocacy where I can get a number with the client. any options for surveys? Without having some server side code elsewhere?

    thanks,

    Adrian

    I think you could use ajax to get the number out of the "number taken" column for the appropriate row here: https://secure.sierraclub.org/site/SSurvey

  • Chris Backus:

    I think you could use ajax to get the number out of the "number taken" column for the appropriate row here: https://secure.sierraclub.org/site/SSurvey

    Though there's nothing in the Survey API that allows you to get back the number of responses for a Survey, there is an S-Tag that can do this (S28), so this is a good example of something where you can "roll your own" API.



    Create a PageBuilder page called getSurveyResults and throw this in its contents:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[E28:REPORTING:[[S334:survey_id]]:[[S334:question_id]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    Then call this page like so:



    http://action.sierraclub.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=1234&question_id=5678&callback=?



    You'll of course need to change the survey_id and question_id. The S28 tag reports on the number of people who have answered a particular question, so you need to provide some required question's ID, like, say, the Constituent Registration Information question.



    This little "API" will return JSONP. The result object will be a table, and I'm assuming you only want the actual number. Here's som jQuery that should take care of that:



    $.ajax({
    dataType: 'jsonp',
    success: function(data) {
    var numResponses = $(data.results).find('p:eq(1)').html();
    console.log(numResponses);
    },
    url: 'http://action.sierraclub.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=1234&question_id=5678&callback=?'
    });

  • Noah Cooper:

    Though there's nothing in the Survey API that allows you to get back the number of responses for a Survey, there is an S-Tag that can do this (S28), so this is a good example of something where you can "roll your own" API.



    Create a PageBuilder page called getSurveyResults and throw this in its contents:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[E28:REPORTING:[[S334:survey_id]]:[[S334:question_id]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    Then call this page like so:



    http://action.sierraclub.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=1234&question_id=5678&callback=?



    You'll of course need to change the survey_id and question_id. The S28 tag reports on the number of people who have answered a particular question, so you need to provide some required question's ID, like, say, the Constituent Registration Information question.



    This little "API" will return JSONP. The result object will be a table, and I'm assuming you only want the actual number. Here's som jQuery that should take care of that:



    $.ajax({
    dataType: 'jsonp',
    success: function(data) {
    var numResponses = $(data.results).find('p:eq(1)').html();
    console.log(numResponses);
    },
    url: 'http://action.sierraclub.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=1234&question_id=5678&callback=?'
    });

    Thanks very much Noah. Works well.

    So if I wanted to find the source of a pledge (and so I can then compare their numbers). I could either

    > have separate surveys.

    > have one survey with questions for each possible source (that are hidden from the user on a custom form where I can change the answers depending on some querystring parameter).

    The idea is we want to set up a competition to see what chapter can drive the most survey submissions (which is an RSVP more or less). So they'll all have a link they use.

  • Adrian Cotter:

    Thanks very much Noah. Works well.

    So if I wanted to find the source of a pledge (and so I can then compare their numbers). I could either

    > have separate surveys.

    > have one survey with questions for each possible source (that are hidden from the user on a custom form where I can change the answers depending on some querystring parameter).

    The idea is we want to set up a competition to see what chapter can drive the most survey submissions (which is an RSVP more or less). So they'll all have a link they use.

    A seperate questions. Is there a way to get a count of donations?

  • Noah Cooper:

    Though there's nothing in the Survey API that allows you to get back the number of responses for a Survey, there is an S-Tag that can do this (S28), so this is a good example of something where you can "roll your own" API.



    Create a PageBuilder page called getSurveyResults and throw this in its contents:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[E28:REPORTING:[[S334:survey_id]]:[[S334:question_id]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    Then call this page like so:



    http://action.sierraclub.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=1234&question_id=5678&callback=?



    You'll of course need to change the survey_id and question_id. The S28 tag reports on the number of people who have answered a particular question, so you need to provide some required question's ID, like, say, the Constituent Registration Information question.



    This little "API" will return JSONP. The result object will be a table, and I'm assuming you only want the actual number. Here's som jQuery that should take care of that:



    $.ajax({
    dataType: 'jsonp',
    success: function(data) {
    var numResponses = $(data.results).find('p:eq(1)').html();
    console.log(numResponses);
    },
    url: 'http://action.sierraclub.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=1234&question_id=5678&callback=?'
    });

    Thanks, Noah!

    I'm new to S-tags, but would love to use this feature. Should "E28:REPORTING" be "S28:REPORTING"?

    I tried both, and with the E28 version I get nothing, and the S28 version I see all zeros, even though the survey has responses:

    http://my.rainforest-alliance.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=5061&question_id=6841&callback=?

    Any help you could give would be much appreciated!

    thanks,

    Danielle

  • Danielle Cranmer:

    Thanks, Noah!

    I'm new to S-tags, but would love to use this feature. Should "E28:REPORTING" be "S28:REPORTING"?

    I tried both, and with the E28 version I get nothing, and the S28 version I see all zeros, even though the survey has responses:

    http://my.rainforest-alliance.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=5061&question_id=6841&callback=?

    Any help you could give would be much appreciated!

    thanks,

    Danielle

    Hey Danielle!



    It should be E28 not S28. E-Tags are the same as S-Tags, except they have parameters that are themselves S-Tags, as seen in the code I provided here. The "E" stands for "evaluated" -- the tags are rendered from the inside out, allowing for the parameters to be evaluated before the outer tag itself.



    I took a look at the source of your page and the problem is that you've substituted "survey_id" and "question_id" with actual values for your Survey:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[S28:REPORTING:[[S334:1381]]:[[S334:1424]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    You should be able to copy and paste the code exactly as it is without substituting any values:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[E28:REPORTING:[[S334:survey_id]]:[[S334:question_id]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    This code is written to dynamically retrieve the survey_id and question_id from the URL when the page is loaded. This is what the S334 tag does -- it returns the value of a named GET query parameter.

  • Noah Cooper:

    Hey Danielle!



    It should be E28 not S28. E-Tags are the same as S-Tags, except they have parameters that are themselves S-Tags, as seen in the code I provided here. The "E" stands for "evaluated" -- the tags are rendered from the inside out, allowing for the parameters to be evaluated before the outer tag itself.



    I took a look at the source of your page and the problem is that you've substituted "survey_id" and "question_id" with actual values for your Survey:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[S28:REPORTING:[[S334:1381]]:[[S334:1424]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    You should be able to copy and paste the code exactly as it is without substituting any values:



    [[U8:text/javascript]][[?xx::x[[S334:callback]]x::::[[S334:callback]](]]{"results": "[[T6:[[E28:REPORTING:[[S334:survey_id]]:[[S334:question_id]]:0:0:0]]]]"}[[?xx::x[[S334:callback]]x:::]]

    This code is written to dynamically retrieve the survey_id and question_id from the URL when the page is loaded. This is what the S334 tag does -- it returns the value of a named GET query parameter.

    Thank you, Noah! I was able to get the table containing the count:

    http://my.rainforest-alliance.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=5061&question_id=6841&callback=?

    However, when I tried the jquery in a page, I'm getting nothing:

    http://my.rainforest-alliance.org/site/PageNavigator/test.html

    any advice?

    thank you!

    Danielle

  • Danielle Cranmer:

    Thank you, Noah! I was able to get the table containing the count:

    http://my.rainforest-alliance.org/site/PageServer?pagename=getSurveyResults&pgwrap=n&survey_id=5061&question_id=6841&callback=?

    However, when I tried the jquery in a page, I'm getting nothing:

    http://my.rainforest-alliance.org/site/PageNavigator/test.html

    any advice?

    thank you!

    Danielle

    Danielle,



    It's working! Right now the code simply logs the count to the console -- if you open a console you should see the count there. If you want to do something more with it, like display the value somewhere on a page, you'd need to update the JavaScript.

  • Noah Cooper:

    Danielle,



    It's working! Right now the code simply logs the count to the console -- if you open a console you should see the count there. If you want to do something more with it, like display the value somewhere on a page, you'd need to update the JavaScript.

    Got it!

    Noah, you are a genious. Thank you so much!

    Danielle

  • Noah Cooper:

    Danielle,



    It's working! Right now the code simply logs the count to the console -- if you open a console you should see the count there. If you want to do something more with it, like display the value somewhere on a page, you'd need to update the JavaScript.

    Hey Noah, as usual this is an awesome tip. One problem I'm having though is that I want to load this on a secure page. So I switched the component to an secure component but it is not working now. Any thoughts?

  • Josh Weinstein:

    Hey Noah, as usual this is an awesome tip. One problem I'm having though is that I want to load this on a secure page. So I switched the component to an secure component but it is not working now. Any thoughts?

    Josh,

    Can you share a link to the page you're having problems on?

  • Noah Cooper:

    Danielle,



    It's working! Right now the code simply logs the count to the console -- if you open a console you should see the count there. If you want to do something more with it, like display the value somewhere on a page, you'd need to update the JavaScript.

    Hi Noah,

    I have one more question for you. How can I capture the responses for two or THREE surveys in this code and have the numResponses be the combined number?

    Thanks!

  • Old thread but still good for the rest of us using Survey.


    I've followed the instructions above, getting the GET response but when trying to render it on a page, I can't seem to be getting anything back.


    Tried a few arrangements like swapping out version of Jquery, calling the function in a few ways, noConflict, etc. but seem to not get any returned values. All pages are set to reusable and Secure https (and i'm calling the secure URL, which rendered correct for Raw Data) What do you think may be missing??

    Raw Data:
    https://secure.americares.org/site/SPageServer/?question_id=4804&survey_id=3201&pagename=zTestRenderingSTag_getSurveyResults&pgwrap=n&callback=?

    Rendered Results on a Page:
    https://secure.americares.org/site/SPageNavigator/zTestRenderingSTag_RenderedResults.html



    Appreciate any help anyone may have


  • M OConnell:

    Old thread but still good for the rest of us using Survey.


    I've followed the instructions above, getting the GET response but when trying to render it on a page, I can't seem to be getting anything back.


    Tried a few arrangements like swapping out version of Jquery, calling the function in a few ways, noConflict, etc. but seem to not get any returned values. All pages are set to reusable and Secure https (and i'm calling the secure URL, which rendered correct for Raw Data) What do you think may be missing??

    Raw Data:
    https://secure.americares.org/site/SPageServer/?question_id=4804&survey_id=3201&pagename=zTestRenderingSTag_getSurveyResults&pgwrap=n&callback=?

    Rendered Results on a Page:
    https://secure.americares.org/site/SPageNavigator/zTestRenderingSTag_RenderedResults.html



    Appreciate any help anyone may have


    It looks like when content was migrated to the new Community a while back, some of the code above got changed to emojis! Change:


    [[?xx::x[[S334:callback]]x::::smileyhappy:]]


    ... to:


    [[?xx::x[[S334:callback]]x::::)]]
  • Ha! I had assumed it was a symbolic "it works!" that was added in the code. Appreciate your answering such an old thread, Noah!

Categories