Created
June 26, 2025 02:14
-
-
Save gxclarke/81fa3e5bab0da334f5784be115094447 to your computer and use it in GitHub Desktop.
Elementor counter with ajax (not working)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
const counters = [ | |
{ | |
id: 'counter-problems-solved', | |
key: 'practiceTotalQuestionCount', | |
}, | |
{ | |
id: 'counter-concepts-scaffolded', | |
key: 'conceptScaffoldedCount', | |
}, | |
{ | |
id: 'counter-concepts-unlocked', | |
key: 'conceptUnlockedCount', | |
}, | |
]; | |
function log(text) { | |
console.log(`ScootPad Learning Impact: ${text}`); | |
} | |
async function fetchImpactMetrics() { | |
try { | |
const response = await fetch('https://scootpad.com/rest/static/impact'); | |
if (!response.ok) throw new Error(`HTTP error: ${response.status}`); | |
return await response.json(); | |
} catch (error) { | |
console.error('Error fetching impact metrics:', error); | |
return null; | |
} | |
} | |
function overrideAndTriggerCounter($widget, value) { | |
log('override'); | |
const $number = $widget.find('.elementor-counter-number'); | |
if (!$number.length) return; | |
$number.attr('data-to-value', value); | |
$number.text('0'); | |
log('trigger'); | |
elementorFrontend.elementsHandler.runReadyTrigger($widget[0]); | |
} | |
function updateAllCounters(data) { | |
log('update'); | |
counters.forEach(({ id, key }) => { | |
const $widget = $('#' + id); | |
if (!$widget.length) return; | |
const value = data.concept_summary[key]; | |
if (value !== undefined) { | |
overrideAndTriggerCounter($widget, value); | |
} | |
}); | |
} | |
function initCountersWhenReady() { | |
fetchImpactMetrics().then(data => { | |
if (!data) return; | |
updateAllCounters(data); | |
}); | |
} | |
log('init'); | |
$(window).on('elementor/frontend/init', initCountersWhenReady); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment