Why Uncaught SyntaxError: Unexpected token ','? - google-analytics

Why I'm getting this error. I have this code in my Nextjs app Head component.
{/* <!-- Global site tag (gtag.js) - Google Analytics --> */}
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXX-X" />
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('set', {'user_id', '${userData?.id}'})
gtag('js', new Date());
gtag('config', 'UA-XXX-X', {
page_path: window.location.pathname,
}); `,
}}
/>

Just to give an answer to the question that otherwise seems unresolved.
This line:
gtag('set', {'user_id', '${userData?.id}'})
should be:
gtag('set', {'user_id' : '${userData?.id}'})

Related

basic GA4 purchase tag not tracking

I'm on GA4 and this basic tag is not showing in my google analytics
gtag("event", "purchase", '.\yii\helpers\Json::htmlEncode([
"transaction_id" => $payment_id,
"value" => $value,
"currency" => $currency,
]).');
here is my gtag
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXX');
</script>
Any idea why? the values are all there when i echo them, but it's still not tracking.

How can i add google analytics.js to Quasar SSR application?

I just want to add google analytics to my quasar ssr application.
I have just updated the head tag in the index.template.html. Is it enough for ssr application?
<!DOCTYPE html>
<html>
<head>
<script
async
src="https://www.googletagmanager.com/gtag/js?id=..."
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "my-code");
</script>
</head>
<body>
<!-- DO NOT touch the following DIV -->
<div id="q-app"></div>
</body>
</html>

How can I set google analytics referrer manually with gtag script?

https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#referrer
I can set google analytics referrer manually with ga script.
but I want to use gtag script for GA implementation.
So How can I set the Referer manually using gtag script?
I tried this. but events are not hit with the referrer I set.
<!-- Add gtag -->
<!-- Global site tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
window.gtag('config', 'UA-160375581-4', {
send_page_view: false,
});
window.gtag('set', { referrer: 'https://child.com' });
</script>
Do you need to use the gtag function? Can you load analytics.js and use the ga function?
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'referrer', 'http://example.com');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

How to combine Google Analytics and Google AdWords gtag.js?

My Google Analytics code is:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-119899800-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-119899800-1');
</script>
Google AdWords gives me the same code like:
<!-- Global site tag (gtag.js) - Google AdWords: 796207283 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-796207283"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-796207283');
</script>
Where & how to locate those code together?
You should merge codes in this way:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-119899800-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-119899800-1');
gtag('config', 'AW-796207283');
</script>
and insert it in <head> of pages you need.
https://support.google.com/google-ads/answer/7548399?hl=en

Google Analytics - my goal event doesn't work

I have a goal in Google Analytics that does not work for a reason that I do not understand. Conversions are not counted.
What could be the problem?
Here's how I set the goal in the site code:
<!DOCTYPE html>
<html lang="ru">
<head>
<!— Global site tag (gtag.js) - Google Analytics —>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108002615-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-108002615-1');
</script>
</head>
<body>
<button onclick="gtag('event', 'best-goal-666', { 'event_category': 'Category', 'event_action': 'Action', 'event_label': 'click-button-347-2', 'value': 1});">RABOTAI BLYAT</button>
</body>
</html>
Screenshot of goal settings in google analytics
As it turned out there was no problem with the code.
Google Analytics counted the conversion after three days.
So if you have similar issue just try to wait for GA's periods will finish.

Resources