A site-wide script is a snippet of code that is to be included on every page of a site. The most popular site-wide script is Google Analytics. Follow the steps below to add a site-wide script to your site.
Important! Google Analytics requires an account for use. Do not use a personal Google account (unless you want to share login info with your boss!). Instead, create an official department account and share the login with others in the department as needed.
Step 1: Create a Block
Blocks should be kept in a _blocks folder at the base level of a site. If your site does not have a _blocks folder, create one before proceeding (change folder settings to disable navigation and disable publishing).
- Select the _blocks folder in the asset tree, then click Add Content > Block
- For Block Type select Text, then click Choose at the upper right
- Enter a relevant Text Block Name (e.g., google-analytics, site-scripts); follow the requirements provided in the interface
- Scroll to Block Content and paste the script into the text editor; see important note regarding Google Analytics above
- Submit to save
Step 2: Edit the Setup Block and Choose the Script Block
- Select the Setup Block in the asset tree and then click Edit
- Scroll to the last setting for Additional Code; select Yes to enable
- Expand the Code section; two script locations are available, In Head (i.e., within the <head> HTML tag) and Before Closing Body (i.e., just before the </body> HTML tag); select the appropriate location based on script instructions
- Choose Block by browsing and selecting the block in the _blocks folder
- Submit to save
Step 3: Publish Full Site
You will never need to publish any blocks (script or Setup), however you will need to perform a site-wide publish to add the script to the published version of every page. Moving forward, all new pages will automatically include the script when published.
There is already a block used in the setup, should I create another one for my script?
If the existing block uses the same location where your script needs to go (i.e., either in the head or body HTML tag), you do not need to create another block; simply amend the existing block by pasting in the new script below any existing code (be careful not to modify any existing code).
If the existing block does not use the same location where your script needs to go, create a new block. In the setup, click the + sign near Additional Code to add a second block, then browse to choose the new block.
Note: Each site-wide script location (head and body HTML tags) should only contain one block each; additional scripts should be added to these blocks directly.
I added Google Analytics and it broke my site.
This will happen if you don't make the adjustment specified above for Google Analytics code. Newer code is missing the async="true" value which is required in the CMS. Find async in the code and include ="true" as in this example:
<script async="true" src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
After the adjustment, your site should function as normal.
Can I add a script to one page rather than to the entire site?
Yes, this is possible.
- Complete Step 1 as instructed above
- For Step 2, instead of editing the Setup Block, edit the page where you want to add the script
- Click the Configure tab, then Choose Block for the appropriate location:
- Additional Optional - Head Tags - Add script to <head> HTML tag
- Additional Optional - Script Tags - Add script before closing </body> HTML tag
- Do not modify any other settings on page; submit to save
- Publish the page