I have been working to track the user visiting my website with their userID. I am able to see the user by using user explorer in the GA View. But i want to fetch the data over the API to do achieve this i came to know that we have to implement custom dimensions in Google Analytics and then by using that dimension we can fetch the data over the API as well. I have tried sending the data using three ways but none of them seems to be working. 1. Universal Analytivs 2. GTAG.JS 3. Google Tag Manager I have tried to debug the data as well using Google Tag Assistant Chrome Extensions and screenshots are attached below.

Snippet 1 using Google Universal Analytics

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', 'UA-125456669-1', 'auto');

ga('send', 'pageview');
ga('set', 'userId', '@Session["CompanyCode"].ToString()#@Session["MembersId"].ToString()'); // Set the user ID using signed-in user_id.
ga('set', 'dimension1', '@Session["CompanyCode"].ToString()#@Session["MembersId"].ToString()');
ga('set', 'dimension2', '@Session["CompanyCode"].ToString()');

Snippet 2 using Google TAg Manager.js

        var dataLayer = [{
            'userID': '@Session["CompanyCode"].ToString()#@Session["MembersId"].ToString()',
    <!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
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-N59M6H8');</script>

But one i went to Behaviour > All Page and Select UserID from Secondary Dimension no record is shown

  • They need to be sent with an existing hit, or "set" prior to the pageview command to send with all hits on the page developers.google.com/analytics/devguides/collection/… Same applies to GTM, you would send them along with a pageview or event. – Bronwyn V Sep 13 '18 at 1:32
  • @BronwynV Followed your instruction and data is being set first and then i am sending pageview. Should i wait for 24 hours or it will start showing result immediately ? – Hasnain Hayder Sep 13 '18 at 6:59
  • @BronwynV Can you help me please for setting this up using Google Tag Manager – Hasnain Hayder Sep 13 '18 at 17:07
  • I'm not good with explaining the GTM stuff, even though i know how to set them up. Not sure if the following page in the Help Center is helpful to you or not support.google.com/tagmanager/answer/6164990?hl=en – Bronwyn V Sep 15 '18 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.