Solved

Counting widgets on a page

  • 24 March 2023
  • 6 replies
  • 54 views

Userlevel 3
Badge +3

I am trying to figure out if i can count somehow how many widgets are on a page.

Context - Customers can add chart widgets to a page, do some config, then they have them on their dashboard.

I can count the clicks on the “add widget button”, I can count the widgets they choose, and I know if they have added it to the page.

What i was trying to work out is how may people have on their pages. We had a hypothesis that people would use multiples of the same widget just configured slightly differently. 

Unfortunately I am sort of stuck on the counting bit.

 

Thanks

Mike

 

 

 

icon

Best answer by jonathan 26 March 2023, 00:31

View original

6 replies

Userlevel 5
Badge +3

@Plasticlizzard You can count the number of elements by selector on a page using querySelectorAll and length. If your widgets all have a class of .widget and you have 10 widgets on the page, document.querySelectorAll(‘.widget’).length would return 10

Add this as a Snapshot on an event that targets the pages containing your widgets. Assuming the widgets are present on the page on page load, this will capture the number returned by that one-liner.

If your widgets lazy load after the page loads (and so aren’t available for capture on a standard pageview), you might want to take a look at this example of firing a track API call in a snapshot after a delay and sending a custom event with num_widgets as a property after a suitable delay.

Userlevel 3
Badge +3

Thanks Jonathan I will give that a try today.

Userlevel 3
Badge +3

Hi Jonathan,

I think i might be getting a bit lost. So I had the “lazy” load issue like you mentioned. So I fixed that with your suggestion. Now i get an extra event when i look at the live feed. That event has the correct widget count which is great, but i can’t seem to be able to chart it. I can define it as an event but then i only seem to be able to map occurrences, not show how many were in the count i can’t see it in the properties.

SNAPSHOT

 

Events in live feed

 

Widget event property

 

 But when i try to chart the event i can count it, but never see the number property.

Should i do something different in the function just to return the number? I can’t work out what I am missing. Any help would be greatly appreciated.

 

 

Userlevel 5
Badge +3

You’re doing the right thing, it may just have been still in process of getting ingested into our metadata store (which feeds the dropdowns). I peeked the backend and you should be seeing `number` and `Number of widgets` now in the dropdowns. Try a refresh if you don’t see it. 

Userlevel 3
Badge +3

Thanks Jonathan,

So i now see the “number” so i can use that in a chart. But now the widgets event does not seem to fire anymore when i load the dashboard. When i look at the live data feed I get the page load but no longer the second “widget counter” event. 
 

Sorry if this is taking up your time. It’s really important for us to see if our idea was working.

Userlevel 5
Badge +3

No worries, I’ll drop you a quick email so we can get into it a little deeper. 

Reply