Building a Shopify Rails application using ScriptTags integration

I’m working on a Rails application that’s hosted on Heroku and needs to integrate with Shopify stores. My goal is to execute JavaScript code on the frontend of different Shopify stores through my app.

I came across some documentation about ScriptTags but I’m having trouble understanding the implementation details. Specifically, I’m confused about where to place this API call:

HTTP POST http://store-name.myshopify.com/admin/scripttags

<?xml version="1.0" encoding="UTF-8"?>
<script-tag>
  <src>http://mysite.com/my-custom-script.js</src>
  <event>onload</event>
</script-tag>

I need help figuring out what URL I should use for the src attribute when pointing to my Rails application, and where exactly should I place the JavaScript function that needs to be called? Any guidance on the proper setup would be really helpful.

It’s clearer once you get the flow. Your Rails app authenticates with Shopify through OAuth, then uses that access token to POST and create the script tag. Point the src URL to a static JavaScript file on your Rails app - like https://yourapp.herokuapp.com/assets/shopify-integration.js. Drop this file in your public directory or serve it through a controller that renders JavaScript. Your actual JavaScript code goes in this file, not the API call. The API call just tells Shopify to load your script on their storefront. Make sure your JavaScript handles cross-origin requests since it runs on Shopify’s domain but loads from your Rails app.

When integrating ScriptTags in your Rails app for Shopify, ensure that the src attribute directs to a publicly accessible JavaScript file on your application. It’s advisable to create a dedicated controller action that serves this file returning ‘application/javascript’ as the response type. For instance, a URL such as ‘https://yourapp.herokuapp.com/shopify/script.js’ would be appropriate. Set up the ScriptTag during the app installation process, right after the OAuth flow. Additionally, remember that your JavaScript file must be served over HTTPS due to Shopify’s requirements, and any API calls should be made from your backend, using the appropriate access token for the shop.

same issue i had last week! make that POST from your Rails backend after OAuth, not the front. put your js in app/assets/javascripts and use the asset link like https://yourapp.herokuapp.com/assets/your-script.js for src. don’t forget to precompile assets on heroku or it might break!