How to automate Slice Machine analytics

Hello!

I wonder is there any plans for SM analytics. What I have in mind.

In our project we have over 60 slices and multiple of slices have variations, some of them example hero have 11 variations. The problem we are currently tackling is that all slices need maintenance.

Currently we export all our docs with external scripts calculate how many times slice and their variation are used in docs. Which gives us a nice overview, that some of our slices are not used anymore and should be revisited. This can end up to redesigning slice, deprecating it or deleting it.

If there is no plan maybe you could give some feedback what else should be tracked and how can we automate this job? Currently we are doing it by hand, which is not great. :smile:

Currently we are tracking

  • How many times slice to get most used slices.
  • How many times slice variation is used.

Hi @juskeviciusarn,

Thanks for posting this idea!

Currently, there are no plans for analytics like this. (Not to say it won't happen, just that it's not currently planned.) Personally, I like the idea of doing this with a custom script, because you can customize the implementation to your liking. (I use a small script to see where Slices are used in our documentation.)

Nonetheless, I'll share this with the product team as a feature suggestion :slight_smile:

Best,
Sam

Hey @juskeviciusarn,

Sorry, I re-read your post, and I realized that I missed your question about how to automate this process.

Yes, I think it should be possible to automate this process! I hope that the script that I shared might serve as a useful starting point. You could potentially write a script like that to run once a week and send a notification with the data you need. I'll leave this thread open in case anyone else has suggestions.

Sam

Hello @samlittlefair
I didn't know about prismic-cli v6 version which has functionality to get all docs. Your demo helped a lot. Big thanks! I already have POC. I ended up with something like this.

// This code is runned on next.js API
import * as prismic from '@prismicio/client'

//
const ACCESS_TOKEN = ''
const REPO_NAME = ''

const SLICE_MAP = new Map()

const sortByNumber = (a, b) => b[1] - a[1]

const checkDoc = async ({ data }) => {
  data?.body?.forEach(slice => {
    if (!SLICE_MAP.has(slice.slice_type)) {
      return SLICE_MAP.set(slice.slice_type, {
        usage: 1,
        [slice.variation]: 1
      })
    }

    SLICE_MAP.set(slice.slice_type, {
      ...SLICE_MAP.get(slice.slice_type),
      usage: SLICE_MAP.get(slice.slice_type).usage + 1,
      [slice.variation]: SLICE_MAP.get(slice.slice_type)[slice.variation]
        ? SLICE_MAP.get(slice.slice_type)[slice.variation] + 1
        : 1
    })
  })
}

const GetSlicesStats = async (req, res) => {
  const endpoint = prismic.getEndpoint(REPO_NAME)
  const client = prismic.createClient(endpoint, {
    accessToken: ACCESS_TOKEN
  })

  const all = await client.dangerouslyGetAll()

  const docs = await all.map(doc => {
    checkDoc(doc)
  })

  const sortedByUsage = new Map([...SLICE_MAP.entries()].sort(sortByNumber))

  console.log('Checked docs', docs.length)
  console.log(sortedByUsage)

  res.end()
}

export default GetSlicesStats

which returned me something like this

 'nav_header' => { usage: 128, 'default-slice': 128 },
 'hero_with_features' => { usage: 28, 'default-slice': 28 },
 'info_block' => {
    usage: 272,
    'default-slice': 236,
    longWithColumns: 12,
    withVideoModal: 24
  },
 ...
1 Like

@juskeviciusarn Awesome! I'm glad that the demo helped and that client v6 made things easier. Thanks for sharing your solution :slight_smile: