23
Views
3
Comments
Solved
How to pass User ID data for the Google Analytic (GA4) using Javascript?
Question

Hi, I did write the code inside the OnApplicationReady action in Reactive web, which passes the User ID info from AAD for the Google Analytic (GA4) side to consume. Below is my code:

try{window.dataLayer.push({'event': 'Login','uid': $parameters.UID,'user_department': $parameters.Department});} catch (error) {console.log("Unable to push data layer to GA")}

Is my way & code the correct one? Because the GA4 does not capture the data.



2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP
Solution

Hi,

Did you ensure the datalayer object exists on the window object?

If not you should create it first:

window.dataLayer = window.dataLayer || []; 

Regards,

Daniel

UserImage.jpg
vikas sharma
Champion

Hello Syaz,

If GA4 not capturing the data then there may be many reasons for it. I guess it not specific to User Id. I think for testing you can pass some static data first and check that GA4 capturing that data or not. If not then issue is with GA4 implementation. Then we need to check issue in that way.

regards

2022-06-03 09-46-04
MUHAMMAD SYAZANI BIN MOHD FAUDZI

Hello Vikas Sharma,

Yes, I did some testing by passing the data for the page records. Meaning that when the user login into the website it will record which page the user has viewed it. This is implemented by using different codes in the JS and the GA4 is correctly capturing it.

2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP
Solution

Hi,

Did you ensure the datalayer object exists on the window object?

If not you should create it first:

window.dataLayer = window.dataLayer || []; 

Regards,

Daniel

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.