Skip to content

AJAX transitions and your own scripts ​

With AJAX transitions, only the first page a visitor opens is loaded in full. Every page after it arrives without a reload: the header, the content and the footer are swapped, and the head stays. A script in the head therefore runs once, on that first page. Code that must run on every page goes in js/custom.js, inside onPage().

How to access ​

HTML/js/custom.js

js
function onPage() {
  // runs on the first page and after every AJAX transition
}
onPage()
document.addEventListener('arts-ajax:settled', onPage)

Every page loads this file last, after the template's scripts.

Running code on every page ​

Write it inside onPage(). The file calls it once when the first page loads, and again each time a visit has finished and the new page is in place (the arts-ajax:settled event). Inside it, document is the page being shown, so a document.querySelector() finds the new page's content.

js
function onPage() {
  // runs on the first page and after every AJAX transition
  console.log('Now on', document.title)
}

Code outside onPage() runs once, on the first page: the place for something set up once for the whole visit.

Running code on one page ​

A <script> written inside a page's content runs only when that page is loaded in full, not when a visitor arrives at it through an AJAX transition. Put the code in onPage() instead, behind a check for something only that page has. Here, the founder's section of studio.html:

js
function onPage() {
  // runs on the first page and after every AJAX transition

  // Studio only
  if (document.querySelector('.studio__founder')) {
    // your code for the Studio page
  }
}

Check for a part of the page's content, not for its address: the same code then works whether the visitor opened the page first or arrived at it from another.

Adding analytics ​

Every page → <head>

Paste the service's snippet into the head of every page with one search and replace, as Edit every page at once shows, for instance before the line of js/custom.js. The snippet counts the first page itself. After that, AJAX transitions send a page view to Google Analytics, the Meta Pixel and Yandex Metrica after every visit, with the page's address and title, when the service's snippet is on the page. Each service needs one setting, so that no visit is counted twice:

ServiceSetting
Google AnalyticsIn the web data stream's Enhanced measurement settings, under Page views → Show advanced settings, turn off Page changes based on browser history events. It is on by default.
Meta PixelAdd fbq.disablePushState = true; inside the base snippet's <script>, before fbq('init', …). The pixel reads it once, as it loads.
Yandex MetricaNothing to change. Do not add defer: true to the tag's options, which Yandex's guide for single-page sites suggests: here it would leave the first page uncounted.
html
<script>
  !function(f,b,e,v,n,t,s){…}(window, document,'script','https://connect.facebook.net/en_US/fbevents.js');
  fbq.disablePushState = true;
  fbq('init', 'YOUR-PIXEL-ID');
  fbq('track', 'PageView');
</script>

The template sends Google Analytics its page views through the gtag() function of Google's own snippet. A site that loads Google Analytics through Google Tag Manager sets its page views up in the container instead.

Next: Contact form.