Pull figures directly from Google Analytics

Could you add to the visitor counter or create a new one that links to GA? rather than wait for the figures to grow from installation of the app you would start with real historical data.

2 Likes

Hi there, @Graham_Diggins :wave:

Glad to say that you can set the needed Total Starting Value on the Info tab of your widget’s settings:


Check it out and let me know if it helped :slightly_smiling_face:

1 Like

Hi Max,

That doesn’t really help as whilst it sets a total the YTD & Month are still set to the date the widget was installed see below.
Also the counter is clearly not working as it shows 0 visitors yesyerday GA says 83

2 Likes

Got it, thanks!

Could you please send me a link to the page, where your widget is installed?

1 Like

Hi Max,

Apologies I have been away, here is the link to my test widgets page, the site is live but this page isn’t linked to from anywhere on the site Widgets | Simpson West

Best
Graham

2 Likes

thanks

Hi there, @Graham_Diggins :wave:

I’ve talked to the devs, and, unfortunately, there is no way to pull the info from the Google Analytics at the moment. I am sorry!

I’ve moved this idea back to the Wishlist and if it gets more support from other users, we’ll try to think it over :slightly_smiling_face:

Thanks Max,

That’s a pity. Could the existing code be modified to allow ALL the figures selected to be set with an initial value? I could then manually copy across once from GA then rely on the daily visits to update the other values. However that would require the daily visits counter to work which at the moment it isn’t.

Apologies for the delayed response!

Do you want to set the starting value just for yesterday, this week/month/year info or for the days in the graph too?

Hi Max,

It would be ideal if I could have a starting value for every box including today. then it should just continue on updating from that point with all the figures matching. The issue would be that the today figure which would be the basis of all the others is not updating correctly.

Thanks in advance
Graham

1 Like

Thank you, I’ll check it with the devs :slightly_smiling_face:

Thank you for waiting, @Graham_Diggins :wave:

Please add this code to the Custom JS field on the Style tab of your widget’s settings:

const TODAY = 1;
const YESTERDAY = 2;
const TWO_DAYS_AGO = 3;
const THREE_DAYS_AGO = 4;
const FOUR_DAYS_AGO = 5;
const FIVE_DAYS_AGO = 6;
const SIX_DAYS_AGO = 7;
const SEVEN_DAYS_AGO = 8;

const WEEK = [SEVEN_DAYS_AGO, SIX_DAYS_AGO, FIVE_DAYS_AGO, FOUR_DAYS_AGO, THREE_DAYS_AGO, TWO_DAYS_AGO, YESTERDAY, TODAY];

const MONTH = 9;
const YEAR = 10;

const waitForElement = (selector, root = document) => new Promise(res => {
    let i = 0;

    const check = () => {
        const component = root.querySelector(selector);

        if (component) {
            res(component);
        } else if (i !== 150) {
            setTimeout(check, 100);
            i++;
        }
    };

    check();
});

waitForElement("[class*='LayoutOne__LayoutOneComponent-sc']").then((widgetContainer) => {
    const firstCounters = [...widgetContainer.querySelectorAll("[class*='LayoutOne__StyledFirstCounter-sc'] [class*='Counter__CounterCount-sc']")];
    [TODAY, ...WEEK].forEach((day, index) => {
        if (!firstCounters[index]) {
          return;
        }
      
        firstCounters[index].textContent = parseInt(firstCounters[index].textContent) + day;
    });

    const today = new Date();
    const dayOfWeek = today.getDay();

    const adjustedDay = (dayOfWeek + 6) % 7;
    const daysPassed = adjustedDay + 1;

    const thisWeek = WEEK.slice(-1 * daysPassed).reduce((acc, day) => acc + day, 0);

    const secondCounters = [...widgetContainer.querySelectorAll("[class*='LayoutOne__LayoutOneCounters-sc'] [class*='Counter__CounterCount-sc']")];
    [YESTERDAY, thisWeek, MONTH, YEAR].forEach((day, index) => {
        secondCounters[index].textContent = parseInt(secondCounters[index].textContent) + day;
    });
});

In the lines 1-8, 12-13, you can set the needed starting values. Keep in mind, that the JS codes work only upon widget installation, not in preview mode. So, you’ll see the changes only on the website, where the widget is installed.

Check it out and let me know if it helped :slightly_smiling_face:

1 Like

Thanks Max,

It’s now working perfectly, one thing to note is that the “This Week” section is actually the last 7 days this week would imply Mon - whatever day we were on, This is a simple change in the info, messages section changing the “This Week” title to “Last 7 Days”.

Thanks for your help with this
Best
Graham

1 Like

Great, you’re always welcome :wink: