basic GA4 purchase tag not tracking - google-analytics

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.

Related

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 -->

Why Uncaught SyntaxError: Unexpected token ','?

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}'})

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.

How to handle multiple GA, AdWords, Remarketing tags

I'm looking for some info on how to best manage multiple Google AdWords and other tags. My primary question is how to handle both the regular AdWord tag, the standard AdWords Remarketing tags and one of the custom AdWords Remarketing tags I created - say a 180 day RLSA list tag. Do I need a separate tag call (vars, call to the conversion script and the noscript img tag)? Is there a way to combine?
Also - being an ecommerce site - I see that for AdWords I need to add my sales total to the noscript img tag. But what about to the remarketing one? It has a default value of 1.00, instructions make no mention to change that and the js version doesn't have any fields for it so I assume 1.00 is correct?
I am adding my entire code for the receipt of my store which has GA, standard AdWord and as well as the 180 day RLSA tag (which I assume is valid as the basic Remarketing tag). Docs don't seem to ever address multiple tags unfortunately.
<!-- BEGIN: AdWords -->
<script type="text/javascript">
var google_conversion_id = 1002886276;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "DZAUCPS-jQoQhKmb3gM";
var google_conversion_value = 64.98;
var google_conversion_currency = "USD";
var google_remarketing_only = false;
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/1002886276/?value=64.98&currency_code=USD&label=DZAUCPS-jQoQhKmb3gM&guid=ON&script=0" />
</div>
</noscript>
<!-- END: AdWords -->
<!-- BEGIN: Analytics -->
<script type="text/javascript">
(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', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-40148609-3', {
userId: '750fd151-6852-4a35-974d-c99f85f993f3'
});
ga('send', 'pageview', {
'title': 'Receipt'
});
ga('send', 'event', 'Completed Order', {
category: 'Shopping',
revenue: 64.98,
id: 734352106,
total: 64.98,
shipping: 0,
tax: 0,
products: []
});
ga('set', 'ecomm_pagetype': 'purchase');
ga('set', 'ecomm_totalvalue', 64.98);
ga('set', 'ecomm_prodid': [218825388699, 118825388699])
</script>
<!-- END: Analytics -->
<!-- BEGIN: AdWords Remarketing -->
<script type="text/javascript">
var google_conversion_id = 1002886276;
var google_conversion_label = "9NX_CKTrkgoQhKmb3gM";
var google_custom_params = {
ecomm_pagetype: 'purchase',
ecomm_totalvalue: 64.98,
ecomm_prodid: [218825388699, 118825388699]
};
var google_remarketing_only = true;
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/1002886276/?value=1.00&currency_code=USD&label=9NX_CKTrkgoQhKmb3gM&guid=ON&script=0" />
</div>
</noscript>
<!-- END: AdWords Remarketing-->
Thanks for any assistance or official guides to multiple tag management.
The question is old, yet I came here from Google just before I found an answer to a similar problem, so I'll try to share my conclusions. It is best to integrate Google Tag Manager in case of having multiple codes. I consider it to be the best practice for your example.

Resources