0

Get views

by
Published Oct 17, 2025

Get the total number of views a piece of content has.

Script confluence
  • Submitted by hugo697 Bun
    Created 284 days ago
    1
    //native
    2
    type Confluence = {
    3
    	email: string
    4
    	apiToken: string
    5
    	domain: string
    6
    }
    7
    /**
    8
     * Get views
    9
     * Get the total number of views a piece of content has.
    10
     */
    11
    export async function main(auth: Confluence, contentId: string, fromDate: string | undefined) {
    12
    	const url = new URL(`https://${auth.domain}/wiki/rest/api/analytics/content/${contentId}/views`)
    13
    	for (const [k, v] of [['fromDate', fromDate]]) {
    14
    		if (v !== undefined && v !== '' && k !== undefined) {
    15
    			url.searchParams.append(k, v)
    16
    		}
    17
    	}
    18
    	const response = await fetch(url, {
    19
    		method: 'GET',
    20
    		headers: {
    21
    			Authorization: 'Basic ' + btoa(`${auth.email}:${auth.apiToken}`)
    22
    		},
    23
    		body: undefined
    24
    	})
    25
    	if (!response.ok) {
    26
    		const text = await response.text()
    27
    		throw new Error(`${response.status} ${text}`)
    28
    	}
    29
    	return await response.json()
    30
    }
    31