<commit_before>import { GatsbyBrowser } from 'gatsby'

export const onRouteUpdate: GatsbyBrowser['onRouteUpdate'] = args => {
  const location = args.location
  // directly from gatsby-plugin-google-gtag
  const sendPageView = function sendPageView() {
    const pagePath = location ? location.pathname + location.search + location.hash : undefined
    window.gtag('event', 'page_view', {
      page_path: pagePath,
    })
  }

  if ('requestAnimationFrame' in window) {
    requestAnimationFrame(function () {
      requestAnimationFrame(sendPageView)
    })
  } else {
    // simulate 2 rAF calls
    setTimeout(sendPageView, 32)
  }
}<commit_msg>Report article tags and publish time as additional dimensions in google analytics<commit_after>import { GatsbyBrowser } from 'gatsby'
import { gtagOrFallback } from './gtag-or-fallback'

// TODO: this would be the same in pixel or hotjar
// how do we unify that?
const supportedMetaTag = {
  'article:published_time': 'article_published_at',
  'article:tag': 'tags',
}

function collectMetaTags() {
  return Array.from(document.querySelectorAll('meta')).map(meta => ({
    key: meta.getAttribute('property') ?? meta.name,
    value: meta.content,
  }))
}

function pageViewDimensionsFromMeta() {
  return collectMetaTags()
    .filter(({ key }) => key in supportedMetaTag)
    .reduce((acc, { key, value }) => {
      const metaKeyNameAsDimensionName = supportedMetaTag[key as keyof typeof supportedMetaTag]
      return Object.assign(acc, {
        [metaKeyNameAsDimensionName]: value,
      })
    })
}

export const onRouteUpdate: GatsbyBrowser['onRouteUpdate'] = args => {
  const location = args.location
  // directly from gatsby-plugin-google-gtag
  const sendPageView = function sendPageView() {
    const pagePath = location ? location.pathname + location.search + location.hash : undefined
    gtagOrFallback()('event', 'page_view', {
      page_path: pagePath,
      ...pageViewDimensionsFromMeta(),
    })
  }

  if ('requestAnimationFrame' in window) {
    requestAnimationFrame(function () {
      requestAnimationFrame(sendPageView)
    })
  } else {
    // simulate 2 rAF calls
    setTimeout(sendPageView, 32)
  }
}
{'fraction_non_alphanumeric': 0.11691884456671252, 'fraction_numerical': 0.002751031636863824, 'mean_word_length': 3.838137472283814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5885443', 'n_tokens_mistral': 652, 'n_tokens_neox': 646, 'n_words': 155}