How to: import the Gadget API into a Shopify storefront (product quiz)
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 recently-installed Shopify-developed theme (for example, the default Dawn theme)
Advanced Some familiarity with Models and Actions in Gadget. If you are new to Gadget, start with the automated product tagger
tutorial
Step 1: Fork product quiz app
Good news, everyone! Instead of building an app from scratch, you are going to fork an existing product quiz app. This app already contains:
a Shopify connection with the required scopes and data models
data models for managing quiz data, including questions, answers, and recommended products
backend logic for cleaning up data when a quiz is deleted
an embedded frontend for creating and managing quizzes
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.
Quiz data models
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.
Here's a diagram to demonstrate what relationships our models will have with each other:
You need some models to capture the structure of the product quiz:
a quiz has many questions
each question can have multiple possible answers
each answer has a recommendedProduct that relates to a product in a Shopify store
This data model allows us to recommend a product for each question answered by a shopper.
You also need a model to capture user responses to the quiz:
quizResult captures 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 shopperSuggestion model links quizResult records to shopifyProduct records
Keeping product 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.
Roles and permissions
Our quiz app has two different sets of users: merchants building the quiz, and shoppers who take the quiz.
This means we need to manage permissions for both of these roles - shoppers interacting with the embedded admin app get the shopify-app-user role, and shoppers on the storefront are unauthenticated.
Permissions have already been set for this app:
the shopify-app-users role has been granted read and write access to the quiz, question, answer, and recommendedProduct models
unauthenticated shoppers can read data from the quiz, question, answer, recommendedProduct, and shopifyProduct models
unauthenticated users also need to be able to save their responses and 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 Access Control in the left nav.
Step 2: Connecting to Shopify
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.
Because the quiz app was forked, the Shopify connection has already been defined: the read_products API scope and required data models have been selected.
To complete the connection, connect to a Shopify Partners app and install it on a development store:
Copy the Client ID and Client secret back to Gadget. You can + Add App Credentials to the Development environment in Gadget, then click Confirm
Copy the App URL and Redirection URL back to the Shopify Partners dashboard. These can be pasted on the Configuration page of your Partners app
Go back to the Overview page of your Partners app and click Select store to install on a development store
After following the prompts from Shopify, your app should be successfully installed on the 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.
To view the status of your sync:
Navigate to the Plugins page, and click Shop Installs underneath the Shopify connection tile
Once your sync is complete, you can build your first quiz in the shop admin.
Step 3: Making our first Quiz
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 page 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.
Step 4: Embed your quiz in a Shopify storefront
Now we need to place our quiz in the storefront so buyers can take it and see the product recommendations.
The installation process will be different depending on whether your store's theme is using Online Store 2.0 or an older version. If your theme is using Online Store 2.0, you need to set up a theme app extension that allows merchants to place the quiz on an existing page via the storefront editor.
If the current theme is not on Online Store 2.0, you need to manually add code files to the theme to embed the quiz.
Option 1: Theme app extension setup
To set up a theme app extension, you need to make use of a Shopify CLI app. We have a forkable app you can use to get started:
Update the direct script tag in extensions/theme-extension/blocks/quiz-page.liquid to include your app's script tag URL
Direct script tag
The script tag URL can be found in the Installing section of your app's API Reference. Your script tag needs --development added to your app-specific subdomain when working on your Development environment:
34 <button name="quiz-submit" type="submit" class="product-quiz__submit button button--secondary">Get my results</button>
35 </form>
36 </product-quiz>
37</div>
38
39{% schema %}
40{
41 "name": "Quiz",
42 "tag": "section",
43 "class": "spaced-section",
44 "settings": [
45 {
46 "id": "quiz_slug",
47 "type": "text",
48 "label": "Enter quiz id"
49 }
50 ]
51}
52{% endschema %}
Replace "YOUR DIRECT SCRIPT TAG URL HERE" with your Gadget app's script tag so we can use the client
Direct script tag
The script tag URL can be found in the Installing section of your app's API Reference. Your script tag needs --development added to your app-specific subdomain when working on your Development environment:
Head over to the Pages section of the Shopify admin (Online Store -> Pages)
Add a new page for your quiz using the quiz theme template
Now you can go back to your theme editor and search for the quiz page to preview your quiz right in the storefront.
In the embedded admin app used to build the quiz, copy a dash-separated (-) quiz id for the quiz you want to embed (example id: my-product-quiz)
Paste your copied quiz id into the Quiz ID field to the right of the theme editor and click Save
Your quiz should now be available in your storefront - try it out!
Conclusion
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!