4

I developed a Shopify application that i install on Shopify Store and i want my application to add a javascript snippet in all the store pages. For this I want my application to modify the current theme, by using Shopify API, and add the javascript snippet to that theme.

I know of several ways to do it but they have several disadvantage:

  • Add a Shopify Asset/Snippet - The store owner has to manually modify the store's theme to link to the asset/snippet.
  • Add a script tag - I want my javascript to be embedded and not to be downloaded from a different source. A different source means longer page loads and i do not want my site to extend the page load.
  • Add an application proxy - This can also extend the loading time.

Can you suggest on what is the correct way to do it?

3 Answers 3

2

Add a script tag and be done with it. No other pattern offers up the same customer experience. When they install your App, your script is available. When they uninstall your App, their theme reverts nicely. Worrying about the load times of your asset is not worth it if you have done your job, and minimized your code and provided a fast CDN address for it.

In 2022, you get Theme App Extensions for OS 2.0. Do that!! Best way to add JS to a theme/store.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi David, Thanks for your response. A script tag is loaded from an external source. I want the javascript to be embedded somehow in the Theme (or from a snippet).
You asked for a suggestion on the correct way to do it. That is the correct way. If you do not like it like that, you are on your own, dealing with the consequences, which include merchants having their theme messed up, etc.
@DavidLazar Is there a way to add a snippet of code using the shopify API?
See the Script Tag API calls
How to load a script tag? Any example?
0

Are you using the embedded apps SDK? I'm pretty sure they have some option that makes this easier, like you can create a "sidebar" on every page and position it wherever you want (hidden if necessary).

Comments

0

Answering you question: the good way is using ScriptTag. Consider this article on this: https://docs.shopify.com/api/unlinked/using-javascript-responsibly

Still if you need to add code snippet as an asset, I want to point out that it's not necessary the customer must manually update the theme. As far as I know, you can add your snippet as an asset and then update another -- theme liquid template. https://docs.shopify.com/api/asset#update

For the reasons described in an article I've provided above, you should not do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.