@@ -164,7 +164,11 @@ async function fetchPageTrendAggregates(ctx: ComputeContext): Promise<{
164164export const pageTrendsModule : InsightModule = {
165165 key : 'page-trends' ,
166166 cadence : [ 'daily' ] ,
167- thresholds : { minTotal : 100 , minAbsDelta : 30 , minPct : 0.2 , maxDims : 100 } ,
167+ // Share-based thresholds (values in basis points: 100 = 1%)
168+ // minTotal: require at least 0.5% combined share (current + baseline)
169+ // minAbsDelta: require at least 0.5 percentage point shift
170+ // minPct: require at least 25% relative change in share
171+ thresholds : { minTotal : 50 , minAbsDelta : 50 , minPct : 0.25 , maxDims : 100 } ,
168172
169173 async enumerateDimensions ( ctx ) {
170174 const { currentMap, baselineMap } = await fetchPageTrendAggregates ( ctx ) ;
@@ -185,28 +189,40 @@ export const pageTrendsModule: InsightModule = {
185189 if ( ! dimKey . startsWith ( 'page:' ) ) continue ;
186190 const originPath = dimKey . replace ( 'page:' , '' ) ;
187191
188- const currentValue = currentMap . get ( originPath ) ?? 0 ;
189- const compareValue = baselineMap . get ( originPath ) ?? 0 ;
192+ const pageviewsCurrent = currentMap . get ( originPath ) ?? 0 ;
193+ const pageviewsCompare = baselineMap . get ( originPath ) ?? 0 ;
190194
191- const currentShare = totalCurrent > 0 ? currentValue / totalCurrent : 0 ;
192- const compareShare = totalBaseline > 0 ? compareValue / totalBaseline : 0 ;
195+ const currentShare =
196+ totalCurrent > 0 ? pageviewsCurrent / totalCurrent : 0 ;
197+ const compareShare =
198+ totalBaseline > 0 ? pageviewsCompare / totalBaseline : 0 ;
199+
200+ // Use share values in basis points (100 = 1%) for thresholding
201+ // This makes thresholds intuitive: minAbsDelta=50 means 0.5pp shift
202+ const currentShareBp = currentShare * 10000 ;
203+ const compareShareBp = compareShare * 10000 ;
193204
194205 const shareShiftPp = ( currentShare - compareShare ) * 100 ;
195- const changePct = computeChangePct ( currentValue , compareValue ) ;
196- const direction = computeDirection ( changePct ) ;
206+ // changePct is relative change in share, not absolute pageviews
207+ const shareChangePct = computeChangePct ( currentShare , compareShare ) ;
208+ const direction = computeDirection ( shareChangePct ) ;
197209
198210 results . push ( {
199211 ok : true ,
200212 dimensionKey : dimKey ,
201- currentValue,
202- compareValue,
203- changePct,
213+ // Use share in basis points for threshold checks
214+ currentValue : currentShareBp ,
215+ compareValue : compareShareBp ,
216+ changePct : shareChangePct ,
204217 direction,
205218 extra : {
219+ // Keep absolute values for display
220+ pageviewsCurrent,
221+ pageviewsCompare,
206222 shareShiftPp,
207223 currentShare,
208224 compareShare,
209- isNew : compareValue === 0 && currentValue > 0 ,
225+ isNew : pageviewsCompare === 0 && pageviewsCurrent > 0 ,
210226 } ,
211227 } ) ;
212228 }
@@ -218,58 +234,63 @@ export const pageTrendsModule: InsightModule = {
218234 const originPath = result . dimensionKey . replace ( 'page:' , '' ) ;
219235 const [ origin , path ] = originPath . split ( DELIMITER ) ;
220236 const displayValue = origin ? `${ origin } ${ path } ` : path || '/' ;
221- const pct = ( ( result . changePct ?? 0 ) * 100 ) . toFixed ( 1 ) ;
222- const isIncrease = ( result . changePct ?? 0 ) >= 0 ;
237+
238+ // Get absolute pageviews from extra (currentValue/compareValue are now share-based)
239+ const pageviewsCurrent = Number ( result . extra ?. pageviewsCurrent ?? 0 ) ;
240+ const pageviewsCompare = Number ( result . extra ?. pageviewsCompare ?? 0 ) ;
241+ const shareCurrent = Number ( result . extra ?. currentShare ?? 0 ) ;
242+ const shareCompare = Number ( result . extra ?. compareShare ?? 0 ) ;
243+ const shareShiftPp = Number ( result . extra ?. shareShiftPp ?? 0 ) ;
223244 const isNew = result . extra ?. isNew as boolean | undefined ;
224245
246+ // Display share shift in percentage points
247+ const isIncrease = shareShiftPp >= 0 ;
248+ const shareShiftDisplay = Math . abs ( shareShiftPp ) . toFixed ( 1 ) ;
249+
225250 const title = isNew
226251 ? `New page getting views: ${ displayValue } `
227- : `Page ${ displayValue } ${ isIncrease ? '↑' : '↓' } ${ Math . abs ( Number ( pct ) ) } %` ;
228-
229- const pageviewsCurrent = result . currentValue ?? 0 ;
230- const pageviewsCompare = result . compareValue ?? 0 ;
231- const shareCurrent = Number ( result . extra ?. currentShare ?? 0 ) ;
232- const shareCompare = Number ( result . extra ?. compareShare ?? 0 ) ;
252+ : `Page ${ displayValue } share ${ isIncrease ? '↑' : '↓' } ${ shareShiftDisplay } pp` ;
233253
234254 return {
235255 title,
236- summary : `${ ctx . window . label } . Pageviews ${ pageviewsCurrent } vs ${ pageviewsCompare } .` ,
256+ summary : `${ ctx . window . label } . Share ${ ( shareCurrent * 100 ) . toFixed ( 1 ) } % vs ${ ( shareCompare * 100 ) . toFixed ( 1 ) } % .` ,
237257 displayName : displayValue ,
238258 payload : {
239259 kind : 'insight_v1' ,
240260 dimensions : [
241261 { key : 'origin' , value : origin ?? '' , displayName : origin ?? '' } ,
242262 { key : 'path' , value : path ?? '' , displayName : path ?? '' } ,
243263 ] ,
244- primaryMetric : 'pageviews ' ,
264+ primaryMetric : 'share ' ,
245265 metrics : {
246266 pageviews : {
247267 current : pageviewsCurrent ,
248268 compare : pageviewsCompare ,
249269 delta : pageviewsCurrent - pageviewsCompare ,
250- changePct : pageviewsCompare > 0 ? ( result . changePct ?? 0 ) : null ,
251- direction : result . direction ?? 'flat' ,
252- unit : 'count' ,
253- } ,
254- share : {
255- current : shareCurrent ,
256- compare : shareCompare ,
257- delta : shareCurrent - shareCompare ,
258270 changePct :
259- shareCompare > 0
260- ? ( shareCurrent - shareCompare ) / shareCompare
271+ pageviewsCompare > 0
272+ ? ( pageviewsCurrent - pageviewsCompare ) / pageviewsCompare
261273 : null ,
262274 direction :
263- shareCurrent - shareCompare > 0.0005
275+ pageviewsCurrent > pageviewsCompare
264276 ? 'up'
265- : shareCurrent - shareCompare < - 0.0005
277+ : pageviewsCurrent < pageviewsCompare
266278 ? 'down'
267279 : 'flat' ,
280+ unit : 'count' ,
281+ } ,
282+ share : {
283+ current : shareCurrent ,
284+ compare : shareCompare ,
285+ delta : shareCurrent - shareCompare ,
286+ changePct : result . changePct ?? null , // This is now share-based
287+ direction : result . direction ?? 'flat' ,
268288 unit : 'ratio' ,
269289 } ,
270290 } ,
271291 extra : {
272292 isNew : result . extra ?. isNew ,
293+ shareShiftPp,
273294 } ,
274295 } ,
275296 } ;
0 commit comments