WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Adding script in Publish website

Forums > Swift

@BSM  

Hi,

i am building a website using Publish and swift. As I want to have some analytics, I have signed up for google analytics, and got some scripts.

my questions is, how do I add the script to my site, and better, where should I add it so thatvit will cover my whole website?

thx!

staySafeAndHealty

3      

.head node accepts customElements and you should add it there, eg.

.head(for: location, on: context.site, stylesheetPaths: [Path("styles.css?v=13")], customElements: [
                .raw(
                    """
                <!-- Global site tag (gtag.js) - Google Analytics -->
                <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-1"></script>
                <script>
                  window.dataLayer = window.dataLayer || [];
                  function gtag(){dataLayer.push(arguments);}
                  gtag('js', new Date());

                  gtag('config', 'UA-xxxxxxxx-1');
                </script>
                """)
            ]),

4      

@BSM  

thx @artrmz !! do I need to add that script to all .heads? or only in the makeIndexHTML function?

3      

All the pages should have .head node as it includes styles etc so yeah, add it to all. Of course, the best idea would be to create a common .head and reuse it everwhere.

3      

@BSM  

how should I create a reusable .head? I should just add a .head to all makeXHTML functions in my theme, correct?

3      

Well, you can eg. create a function or computed variable that will return .head with all the things you need for each page.

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.