Skip to content

Commit

Permalink
handle chartbeat initial page load better (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
walsh9 authored May 1, 2023
1 parent 99a86e7 commit 83ad25b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions composables/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const useNavigation = () => useState<Navigation>('navigation', () => ({
propertyDescription: ''
}))
export const useStrapline = () => useState<string>('strapline', () => 'A non-profit newsroom, powered by WNYC.')
export const useChartbeatFirstPageLoaded = () => useState<boolean>('chartbeatFirstPageLoaded', () => false)


// audio player globals
Expand Down
56 changes: 33 additions & 23 deletions plugins/chartbeat.client.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
import { nextTick } from 'vue';

export default defineNuxtPlugin(() => {
/* eslint-disable */
(function() {
/** CONFIGURATION START **/
const _sf_async_config = window._sf_async_config = (window._sf_async_config || {});
_sf_async_config.uid = 16246; //CHANGE THIS TO YOUR ACCOUNT ID
_sf_async_config.domain = 'gothamist.com'; //CHANGE THIS TO YOUR SITE ID
_sf_async_config.useCanonical = true;
_sf_async_config.useCanonicalDomain = true;
_sf_async_config.sections = ''; //SET PAGE SECTION(S)
_sf_async_config.authors = ''; //SET PAGE AUTHOR(S)
_sf_async_config.title = document.title; //SET PAGE TITLE
/** CONFIGURATION END **/
function loadChartbeat() {
const e = document.createElement('script');
const n = document.getElementsByTagName('script')[0];
e.type = 'text/javascript';
e.async = true;
e.src = '//static.chartbeat.com/js/chartbeat.js';
n.parentNode.insertBefore(e, n);
let firstPageLoaded = useChartbeatFirstPageLoaded()

const initializeChartbeat = (pageInfo) => {
const _sf_async_config = window._sf_async_config = (window._sf_async_config || {})
_sf_async_config.uid = 16246
_sf_async_config.domain = 'gothamist.com'
_sf_async_config.useCanonical = true
_sf_async_config.useCanonicalDomain = true
_sf_async_config.sections = pageInfo.sections || ''
_sf_async_config.authors = pageInfo.authors || ''
_sf_async_config.title = document.title

const e = document.createElement('script')
const n = document.getElementsByTagName('script')[0]
e.type = 'text/javascript'
e.async = true
e.src = '//static.chartbeat.com/js/chartbeat.js'
n.parentNode.insertBefore(e, n);
}

const updateChartbeat = (pageInfo) => {
if (typeof window.pSUPERFLY !== 'undefined') {
window.pSUPERFLY.virtualPage(pageInfo)
}
loadChartbeat();
})();
}

const updatePage = (pageInfo) => {
if (!process.server && typeof window.pSUPERFLY !== 'undefined') {
window.pSUPERFLY.virtualPage(pageInfo)
if (!process.server) {
if (firstPageLoaded.value === false) {
firstPageLoaded.value = true
initializeChartbeat(pageInfo)
} else {
updateChartbeat(pageInfo)
}
}
}

return {
provide: {
chartbeat: {
Expand Down

0 comments on commit 83ad25b

Please sign in to comment.