Update Shopify Theme Programmatically

StevenPetteruti
New Member
7 0 0

Hello, I'm looking to add a bit of tracking code to a specific page on every store that installs my shopify app. What I'd like to do is inject a bit of code, above the </body> tag in the theme.liquid file. I am curious on two parts, one: is there a javascript based library that allows me to parse liquid files from a string (as that is what I'll get back from the shopify endpoint), and insert a snippet like the following. And secondly, is this considered good practice, provided I remove my appended code upon uninstalling.

 

<script>
    // this is my code
</script>

 

My plan is to hit

GET /admin/themes/#{theme_id}/assets.json?asset[key]=templates/index.liquid&theme_id=${id}

 However I then need some way to parse the response for that and then call

PUT /admin/themes/#{theme_id}/assets.json
{
   "asset":
   {
      "key": "templates/index.liquid",
      "value": MY_APPENDED_LIQUID
   }
}

My second question, is where do I get the current theme id to pass into the above GET request?

0 Likes
mkamalkayani
Tourist
8 2 1

There is an endpoint `scriptTag` which stores links to the remote javascript files. These scripts are added to theme.liguid file by shopify at `contnet_for_header` object.

Benefit of this approach is that when an app is uninstalled the scriptTags added by the app are automatically removed.

There is a blog post that explains this process.
 https://www.shopify.com/partners/blog/add-code-from-your-app

 

0 Likes