We now have a second grade event that represents an actual click on one of our emails.
So we're going to look at this event right here and figure out how we can tell which survey the user
just voted on and then make sure we update not only the feedback property on that survey.
Remember the yes or no property but also make sure that the particular user gets marked as having given
us a response.
So we've got a pretty good amount of work here.
Let's get to it.
Let's first start off by inspecting this event that we got back from second grade a little bit more
closely.
You'll notice that the event itself has a type of click.
So yes it's definitely a click on the link.
We can also see who clicked on the link.
Here's the email property and then we also get the URL that the user was attempting to visit.
Now there's just one piece of the puzzle missing here.
You'll notice that nothing inside this event is currently telling us anything about the survey that
the user is responding to.
And it's also not telling us whether or not the user clicked on the yes or no link inside the e-mail.
So I think that we still need to do a little bit of work here.
First off let's kind of just make things really crystal clear right now.
So we've got some number of events coming from our sin grid web poll data.
We know the users e-mail but nothing else.
So if we have a big old list of surveys inside of our database right now we don't know if the user just
clicked on a survey or an e-mail corresponding to this survey or this one.
And then even then we don't know whether or not the user gave us a yes vote or no vote.
In addition you know you might be thinking Well Stephen that's fine maybe we can look at all the different
surveys and find the survey where the user's email address is marked as the recipient.
Well that would definitely work out.
But what happens if a given user was included on two different surveys.
So maybe this recipient and this recipient right here are the exact same person.
So we can't really just rely upon the user's e-mail for figuring out what survey they are belonging
to either.
So we need to think of a better approach for identifying what survey a user is responding to.
Here's my suggestion.
Here's where I think we should do right now.
Whenever we give a link to a user to click on inside of the e-mail they are going to the route of API
slash surveys slash things and they're always going to the same route no matter what.
What I'm thinking is that maybe we should change up the route that we place inside of those emails.
Instead we'll give them a route or something like API slash survey ID.
So the very particular ID of the survey that they are attempting to look at slash choice.
So in reality we might end up with a route that looks something like API slash ABC 1 2 3.
So that's the route.
I mean the idea of the survey that the user is responding to and then choice would either be yes if
the user clicked on yes or no if the user clicks on no.
So if we make sure that our e-mail kind of encodes this information into the link that the user is clicking
on then when we get the response back from our web host data we can pull out the survey ID to figure
out which surveyed the user was looking at and also figure out whether or not the user clicked on the
yes or no button.
We can then still find the correct recipient and make sure we update them as having responded to that
survey and we can also make sure that we update the surveys.
Yes and no property to record the actual feedback.
So I think this is going to be a pretty good approach here.
Lets take this type of direction to make sure that we update this link right here to kind of encode
this information will go and find our e-mail template.
So the template that we use to kind of populate the body of the emails that we're sending out so all
change back to my code editor a find my services directory inside of there is the email templates folder
and inside of there is the survey template file.
So here are the two links that we are currently generating.
We send the user to either API.
Only Right now I should say to API slash surveys slash.
Thanks and that's the only thing we're really sending user to now.
You will recall that this function this template right here is being passed the survey model.
So we already have access to the ID of this very particular survey.
So in both links right here we're going to delete the word it let's do surveys in here.
Let's let's change this up a little bit.
Let's say that we will still go to slash surveys.
So do API surveys and then the idea of the survey so it could still keep that kind of nice name spacing
here of slash surveys rather than just always going to API slash idea the survey.
So we'll keep those surveys namespace.
So we'll say API surveys slash and will put in the idea of this particular survey.
So survey ID and we're going to do this for both length's survey data.
And then rather than showing just you know thanks on here we'll make sure that we encode whether or
not the user clicks on yes or no.
So in the first link this is the Yes button.
So I want that to be yes.
And then on the second one this would be no down here.
So now whenever we generate this email body and send it out to the actual user the link that they click
on will reflect API.
Slash surveys the idea the survey they are responding to.
And then yes or no.
Let's now test this out through the entire flow and just make sure that we are communicating this information
all the way down the chain.
And once we kind of verify that that information is present We'll then figure out exactly how we pull
the survey ID and the yes or no vote out of the Centigrade event.
Let's test this thing out.
I'm going to go back over to my re-act application.
We're going to create a new survey.
So I'll give this one a title of about a survey ID test that works I'll copy that on down and I'm going
to send it to my test e-mail again and click on next and then send out the survey.
It's now a flip on over to my e-mail account.
Here's that survey I.D. test so now in theory the yes or no buttons are going to kind of include the
idea of the survey that we are responding to and whether or not we're clicking on yes or no for this
first test I had to click on the Yes button.
So I click on yes.
Now we get a message here that says cannot get this particular route.
That's totally fine.
That's just our express server saying hey I don't know what route you're attempting to visit here.
So we will want to make sure that we still show users some nice message of Hey thanks for voting on
our survey or something like that but we'll get around to that in just a second.
Much more importantly you'll notice that our new route now shows the idea of the survey and the fact
that we clicked on the option.
Yes.
So let's now flip over to our terminal and check out the response that we get from the web hook route.
And so when I flip back over I just I timed it perfectly.
Here's the response we got or the web hook data that we got from said grid.
So remember that it always takes about 30 seconds to get your data over here.
So if you don't see the data right away just pause the video for a second until you see the stuff pop
up.
So now inside of this event object that we got passed we were being told the e-mail the person who just
voted and then we also are being told the idea of the survey that they are responding to and the fact
that they clicked on the Yes button.
So it's really now up to you and me to somehow extract the survey I.D. from this thing and also extract
the fact that they voted yes.
So let's take a quick break.
When we come back the next set S..
We're going to figure out how we're going to parse this.
You are all right here and pull out just the parts of the world that we care about.
So quick break.
We'll continue in the next section.