Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding participant demographic data from qualtrix response to raw com… #61

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ interactionId: <parent flow interaciton id>

#### Analytics + Survey Response Export
Exports analytics in bulk that are associated with a survey response. Also uploads
participant data to google spread sheet.
participant data to google spread sheet. Data provided in the request body, as well
as demographic data collected from Qualtrix api will be uploaded. Only responses that
have a Complete status (user has completed the survey) will be uploaded to the google spead sheet.

`POST /survey-export`

Expand Down
32 changes: 30 additions & 2 deletions gdrive/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,39 @@ def delete_file(id: str) -> None:
service.files().delete(fileId=id, supportsAllDrives=True).execute()


def upload_participant(first, last, email, responseId, time, date):
def upload_participant(
first,
last,
email,
responseId,
time,
date,
ethnicity,
race,
gender,
age,
income,
skin_tone,
):
"""
Append participant data to spreadsheet
"""
values = [[first, last, first + " " + last, email, responseId, time, date]]
values = [
[
first,
last,
first + " " + last,
email,
responseId,
time,
date,
ethnicity,
race,
gender,
income,
skin_tone,
]
]

body = {"values": values}
result = (
Expand Down
22 changes: 21 additions & 1 deletion gdrive/export_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async def survey_upload_response(
request: SurveyParticipantModel, background_tasks: BackgroundTasks
):
"""
Single endpoint that kicks off qualtrics response fetching and exporting
Single endpoint that kicks off qualtrics response fetching and exporting. Requests response data
from the Qualtrix API and uploads contact and demographic data to the google drive. Does not upload
responses without a complete status.
"""

background_tasks.add_task(survey_upload_response_task, request)
Expand All @@ -71,6 +73,16 @@ async def survey_upload_response_task(request):

log.info("Response found, beginning export.")

if response["status"] != "Complete":
raise error.ExportError(
f"Cannot upload incomplete survery response to raw completions spreadsheet: {request.responseId}"
)

# By the time we get here, we can count on the response containing the demographic data
# as it is included in the Completed flow responses. Responses without complete status
# throws exception in get_qualtrics_response
survey_resp = response["response"]

if request.participant:
participant = request.participant
client.upload_participant(
Expand All @@ -80,6 +92,14 @@ async def survey_upload_response_task(request):
request.responseId,
participant.time,
participant.date,
survey_resp["ethnicity"],
", ".join(
survey_resp["race"]
), # Can have more than one value in a list
survey_resp["gender"],
survey_resp["age"],
survey_resp["income"],
survey_resp["skin_tone"],
)

# call function that queries ES for all analytics entries (flow interactionId) with responseId
Expand Down
1 change: 1 addition & 0 deletions gdrive/export_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,5 @@ def get_qualtrics_response(surveyId: str, responseId: str):
raise error.ExportError(
f"No survey response found for responseId: {responseId}"
)

return r.json()