How to add custom code to a Shopify theme
A site-wide script on Shopify goes in the theme’s layout file, layout/theme.liquid. That file wraps every storefront page except checkout. The edit is one pasted line, and on the live theme it takes effect the moment you save.
Before you start
- Access to the Shopify admin for the store
- A theme whose code you can edit. A paid theme can be edited only after you have purchased it.
- The script from your tool, as a complete <script> tag
Steps
- Duplicate the themeShopify recommends making a duplicate before you edit code, so you have a backup to go back to.
- Open the code editorIn the Shopify admin go to Online Store. On the theme you want to change, open the … menu and choose Edit code.
- Paste the script inside the head of theme.liquidOpen layout/theme.liquid and find the opening <head> tag. Paste the script on its own line anywhere before the closing </head>.
<head> ... <script defer src="https://example.com/tool.js"></script> </head>An example tag. Paste the one from your tool and leave the rest of the head as it is. - SaveSaving an edit to the live theme puts it in front of shoppers straight away. If you edited the duplicate, the script reaches nobody until you publish that theme.
Check it worked
- Open a product page on the storefront in a private window, view the source and search for the script’s address.
- Repeat on the cart page. Expect it there too, since theme.liquid covers every non-checkout page.
What goes wrong
- The script does not run in checkout
- Theme layouts apply to non-checkout pages only. checkout.liquid is unsupported and the old additional scripts on the Thank you and Order status pages were sunset. Checkout tracking goes through pixels under Settings → Customer events, which run sandboxed and do not accept HTML.
- The script vanished after a theme update
- An update can drop code edits. Reopen layout/theme.liquid after every theme update and paste the line back if it is gone.
- Edit code will not let you change a paid theme
- A paid theme on trial cannot be edited. Buy the theme, or make the change on a theme you own.
- You saved, and the storefront has not changed
- The edit went into an unpublished duplicate. Publish that theme, or make the same edit on the live one.
Checked on 2026-11-11 against Shopify Help Center: Edit theme code, Shopify.dev: Theme layouts.
Increase conversions on your Shopify store by 20%
Dynamite personalizes the page for every visitor. Dynamite personalizes a Shopify storefront from one script line in theme.liquid. Because that file stops at checkout, it neither reads nor changes anything there.
Dynamite for ShopifyQuestions
Does a script in theme.liquid run on checkout pages?
No. Shopify applies theme layouts to non-checkout pages only. Checkout tracking uses pixels set up under Customer events.
Will updating my theme remove the script?
It can. Check the line in layout/theme.liquid after each update.
How do I remove the script?
Delete the line and save. The file’s Timeline also has Restore contents, which takes the file back to an earlier version.