Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Analytics #429

Closed
YourItalianChef opened this issue Nov 27, 2022 · 6 comments
Closed

Google Analytics #429

YourItalianChef opened this issue Nov 27, 2022 · 6 comments
Labels

Comments

@YourItalianChef
Copy link

Hi,
i need to add GTM and google Analytics tag in one script file,
how i can do it, where i can find some example?

@orestbida
Copy link
Owner

If GA is loaded via GTM then this should help you out: #110 (comment).

What do you exactly mean with "one script file"? Is it a dedicated file for GA and GTM's setup or is it the plugin's config. file (cookieconsent-init.js)?

@YourItalianChef
Copy link
Author

it's a dedicate file for it,
i have two codes like
GTM-XXXXXX
G-XXXXXXX,
and i need to give permission on the same time for two.

Only that the second requires a preloaded google script,
while the first can be loaded during.

Another question, when i go to change my choose to decline anl permission, did i need reload page?

@orestbida
Copy link
Owner

There are 2 ways to load GA4, either via gtag.js or via GTM. Since you're already using GTM, I would suggest to simply configure GA4 there.

If you really want to load both gtag and gtm's scripts, then this is how I would do it:
analytics-scripts.js:

// Replace with your codes
const GA4_CODE = 'G-XXXXXXXXXX';
const GTM_CODE = 'GTM-XXXXXXX';

// Load ga4
cc.loadScript(`https://www.googletagmanager.com/gtag/js?id=${GA4_CODE}`, () => {}, [
    {name: 'async', value: ''}
]);

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', GA4_CODE);

// Load gtm
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer',GTM_CODE);

cookieconsent-init.js

cc.run({
    // your config

    onAccept: function () {
        if(cc.allowedCategory('analytics')){
            cc.loadScript('./analytics-scripts.js');
        }
    },
    
    onChange: function (cookie, changedCategories) {
        if(changedCategories.includes('analytics')){
            if(cc.allowedCategory('analytics')){
                cc.loadScript('./analytics-scripts.js');
            }else {
                // disable gtag/ga
                window['ga-disable-G-XXXXXXXXXX'] = true;
                gtag('consent', 'update', {
                    'analytics_storage': 'denied'
                });
            }
        }
    }
});

Another question, when i go to change my choose to decline anl permission, did i need reload page?

Yes, if you want the tracking to stop immediately! You can configure the plugin to reload the page for you:

// ...
toggle: {
    value: 'analytics',
    enabled: false,
    readonly: false,
    reload: 'on_disable'
}
// ...

@nickaws
Copy link

nickaws commented Dec 6, 2022

const GA4_CODE = 'G-XXXXXXXXXX';
const GTM_CODE = 'GTM-XXXXXXX';

// Load ga4
cc.loadScript(`https://www.googletagmanager.com/gtag/js?id=${GA4_CODE}`, () => {}, [
    {name: 'async', value: ''}
]);

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

hello orestbida ,

I copied this setup, but it does not delete _GA_XXXXX, it only deletes _GA on reload.

v2.8.9 making changes AFTER the initial OK, then I want to disable, call javascript:cc.showSettings(); and then disable analytics, click save settings => reloads, and deletes _GA only, if you click "Reject All" at that stage (without saving) then it does delete _GA_XXXXX (xxx being whatever ID google created)

@orestbida
Copy link
Owner

@nickaws,

you can check if the plugin is effectively deleting the cookies by using the src/cookieconsent.js version — instead of dist/coookieconsent.js — which has logs enabled.

You have to execute window['ga-disable-G-XXXXXXXXXX'] = true; to properly disable google analytics, otherwise the _GA_XXXX.. cookie will be recreated. The reason why the cookie is successfully deleted after the second action (as far as I can tell), is because google analytics script is no longer running, and thus it cannot re-set _GA_XXXX...

Make sure you implement the onChange method as above. If the issue still persists, please provide a minimal reproducible demo project (github repo, stackblitz ...).

@stale
Copy link

stale bot commented Jan 6, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 6, 2023
@stale stale bot closed this as completed Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants