When building storefront apps using Liquid, you may need to make requests to a Gadget backend. This guide will show you how to use your Gadget project's direct script tag in the Shopify storefront to embed a product quiz in a store.
Product recommendation quizzes help build engaging sales experiences for online shoppers by allowing them to map their problems or concerns to a product that best meets their needs. For Shopify merchants, this can be an appealing proposition! Merchants can build custom quizzes that present shoppers with a tailored experience, resulting in higher sales conversions and greater satisfaction by matching shoppers with the right products.
Requirements
Before starting this guide you need the following:
→ A Shopify Partners account
→ A development store
→ A recently-installed Shopify-developed theme (for example, the default Dawn theme)
→ Some familiarity with Models and Actions in Gadget. If you are new to Gadget, start with the automated product tagger tutorial
Good news, everyone! Instead of building an app from scratch, you are going to fork an existing product quiz app. This app already contains:
Click here to fork the app:
Here is a diagram showing an overview of the app's tech stack:
The next step will be installing your quiz app on a Shopify store, but first, let's take a look at the data models and permissions that have been added for you.
You need a way to create, serve, and record quiz responses in your app. The recording of responses enables you to track the conversion state mentioned in the intro, effectively making a response to a quiz a snapshot of a session that a shopper has with your quiz.
You need some models to capture the structure of the product quiz. A quiz has many questions, and each question can have multiple possible answers. Each answer has a recommendedProduct that relates to a product in a Shopify store. This means that each question answered will be linked to a product to offer shoppers.
You also need a model to capture user responses to the quiz, quizResult. This will capture shopper information, such as an email address, and a list of recommended products so follow-up emails can be sent once a quiz is completed. The products recommended to shoppers are stored in a separate model, shopperSuggestion. Keeping these recommendations in a separate model means you can update quiz questions and answers while maintaining recommendations for shoppers who took previous versions of the quiz.
Here's a diagram to demonstrate what relationships our models will have with each other:
The quiz app has two different sets of users. The merchant who is creating the quiz, and shoppers who take the quiz. Merchants will create quizzes in their store admin and will have the Shopify App User role in Gadget. Shoppers will be unauthenticated, so all quiz-related data needs to be readable by anyone.
Gadget helps manage these different roles using the Roles & Permissions page, which you can access through the Settings option in the left nav.
Permissions have already been granted to this app. The shopify-app-users role, which is assigned to merchants when building a quiz in the store admin, has been granted read and write access to the quiz, question, answer, and recommendedProduct models. unauthenticated users, which are shoppers, can read data from the quiz, question, answer, recommendedProduct, and shopifyProduct models. unauthenticated users also need to be able to save their responses. They have been granted permission to create new records of the quizResult and shopperSuggestion models.
You can view these permissions by clicking on Settings and then Roles & Permissions in the left nav.
To recommend products to shoppers, we'll need product data in our app that we can map to the results of a product recommendation quiz. Using Gadget's Connections feature, we can connect our app to a Shopify store and pull product data right from the shop.
The Shopify connection provides us with access to any of the models surfaced in Shopify's Admin API, as well as an authenticated client and webhook consumption. This connection also allows us to sync data between Shopify and Gadget, both scheduled and on-demand.
Because the quiz app was forked, the Shopify connection has already been created for you. The read_products API scope has been selected, and the shopifyProduct data model imported into your app.
All that needs to be done to complete the connection is to connect to a Shopify Partners app and install it on a development store. After installing, Shopify product data will automatically be synced to your Gadget app's database via a code effect added to the Shopify Shop install action. You can find this code snippet in shopifyShop/install.js.
Your app should successfully install on the development store.
A code effect that starts a data sync has already been added to the Shopify Shop install action. Once you install your app on a store, product data will be synced automatically. To view the status of your sync:
Once your sync is complete, you can build your first quiz in the admin of the store you just installed on.
Now for the fun part, making a quiz!
After installing the app you will be redirected to the main page for the embedded quiz app in the Shopify admin. Go ahead and create a new quiz - add some questions and answers, and link answers to recommended products.
We can look at records in Gadget and see how our frontend app connects with Gadget through the client and makes API calls against it. If we look at the Quiz data by selecting the Data icon on the Quiz model in the left-hand sidebar, we should see at least one instance of Quiz, including its ID, title, and body. We can also inspect our other records to see how our pieces work together to create our quiz experience.
When you've got a quiz that you're happy with, note the ID of the quiz.
While we used an NPM package to install our client into our freestanding app, we'll need another method of calling the client in our Shopify shop's theme. Gadget allows us to call our API client directly with a script tag.
We only need the client to run to serve the desired product recommendation quiz. In this case, we'll make a new template for the Page resource and then use it on a page we'll create to hold the quiz.
In your Shopify admin for your shop, head to Online Store > Themes and select Edit Code under the Actions menu for the theme you wish to edit.
Under Sections, create a new section called quiz-page.liquid.
We're going to replace this page with the following code:
We just need to replace the "YOUR DIRECT SCRIPT TAG URL HERE" with your Gadget app's script tag so we can use the client. Your app's script tag URL can be found in the Installing section of your quiz app's API Reference docs.
Your direct script tag should be for your Development environment! Your script tag needs --development added to your app-specific subdomain.
If your app url looks like https://test--development.gadget.app/, your script tag src should read
<script src="https://test--development.gadget.app/api/client/web.min.js"></script>
When you deploy your Gadget app to Production and set up a quiz on a shopper-facing store, make sure to use the production URL in your script tag.
Now under Templates, select “Add a new template” and add a template called page.quiz.json. This requires you to select the page template type.
Replace the generated file with the following JSON:
Under the Assets section in the sidebar, select Add a new asset and create a new JavaScript file called product-quiz.js. You can then add the following to that file:
You'll need to make one adjustment here: replace the QUIZ_ID = 1; definition with the ID of the quiz you want to serve. Your Quiz ID can be grabbed from the quiz model's Data page in Gadget. This JavaScript file includes examples of using the GraphQL API (to fetch the quiz) and the JS API (to fetch recommended products) to make requests to your Gadget backend.
Save your changes, and we're ready to go! Head over to the Pages section of the Shopify admin, and create a new page for your quiz. You can set the template to use your new quiz template. View the page to see your quiz right in your Shopify store, ready to recommend products to your shoppers.
Today, we learned how Gadget and Shopify can work together to create engaging buying experiences for shoppers. This collaboration also offers an easy-to-use platform for building your app in less time. You can expand your app by using the Product ID to quickly create a shopping cart on the frontend using Javascript, resulting in a faster buying experience. When deploying to Production, remember to update the script tag in your liquid file to match the Production Gadget environment.
Want to know more about building effortless, expressive apps with Gadget? Check out our Guides and get building today!
Need support? Join our Discord, or book office hours with our Solutions team!