Google Analytics - Match UserID with my site's account ID - google-analytics

Each of my registered clients have an unique account ID (eg: agent n°: 00173393).
I want to retrieve this information through google analytics... It's not a personal information and for statistics use only.
I implemented userID, but how to match userID and the accounts IDs ?
Is it possible to create a variable for the account ID number ?

Why don't you use the account ID as User ID as well? That way you would have a 1-to-1 match:
ga('create', 'UA-XXXXX-Y', 'auto', {
userId: accountId
});
Please note however that the User ID is not exposed back by Google Analytics. If you want to retrieve it, you need to save it inside a custom dimension:
Admin -> Property -> Custom Dimensions -> Create one with User scope
Add custom dimension tracking to your code
For instance:
ga('create', 'UA-XXXXX-Y', 'auto', {
userId: accountId,
});
ga('set', 'cd1', accountId); // if it's custom dimension n1
ga('send', 'pageview');
Then you can retrieve the User ID via the UI:
or the API

Related

How to check ga dimensions(Google Analytics)?

As per my code I am setting the 'dimension1' field.
Defined fields: email(contains user email)
ga('set', 'dimension1', email);
ga('send', 'pageview');
How to see the dimension1 values in GA?
Original requirement: Track id of each user.

meteor-shopify User Creation/ Login after Auth callback

Assuming I want to create users upon authorizing the app, how would I grab their email during the onAuth callback...? Looks like the callback assumes the user is already logged in. Am I thinking about it correctly?
I noticed when installing the Fishbowl Prizes app, after auth I can click on the accounts tab and see that all my account info is pre-populated from my shopify store account (name, email, address, etc).
I'm not sure if I should go by the title or the content of the post in terms of answering your question, so I'll provide a very simple example of how to get the info from the API and do something with it here.
I have provided a more in depth answer related specifically to grabbing the details from the API for user account creation here: https://github.com/froatsnook/meteor-shopify/issues/15#issuecomment-177413630
Looks like the callback assumes the user is already logged in.
The userId param is undefined if there is no user. If your onAuth operations don't need to do anything with the user, you can just leave it out of the params. In your case you'll just want to handle it conditionally using an if/else block:
if(!userId){
// do stuff
} else {
// do other stuff
}
On to the example of grabbing those details from the API:
All the prepopulated information you are seeing is available from the Shopify API in the shop object. You already have the access token when onAuth callbacks are fired, so you can just grab it from the API immediately after you have inserted the shop's Keyset.
For the sake of simplicity, in this example we'll assume the user already exists and is logged in. In your server-side onAuth callback (after you have inserted the keyset) you can do something like this to add those fields to the user's profile object:
Shopify.onAuth(function(access_token, authConfig, userId) {
var shopUUID = uuid.new(); // Not secure to name keyset same as the shop!
Shopify.addKeyset(shopUUID, {
access_token: access_token
});
var api = new Shopify.API({
shop: authConfig.shop,
keyset: shopUUID
});
// get the Shop object from the API
var shopObj = api.getShop();
var userInfo = {
'profile.name': shopObj.shop_owner,
'profile.email': shopObj.email,
'profile.phone': shopObj.phone,
'profile.shopName': shopObj.name
};
Meteor.users.update({_id: userId}, {$set: userInfo})
});
Then you can use them in templates like this:
{{currentUser.profile.name}} or {{currentUser.profile.email}}
Or in functions like so:
var realName = Meteor.user().profile.name
or
var userEmail = Meteor.user().profile.email etc
For a more about using this data for user creation, see my explanation here:
https://github.com/froatsnook/meteor-shopify/issues/15#issuecomment-177413630

How to get statistics of where visitors come from via URL params

There are many QR codes that contains URL of website such as:(it just demos link)
http://www.popupstore.com/index.php?qrcode_type=magazine&location=Singapore
http://www.popupstore.com/index.php?qrcode_type=banner&location=Vietnam
I need a way can summary to know that where customer come from (nearly same as source/channel in Google Analytics):
Type: Mazazine, banner, etc.
Location: Vietnam, Singapore, etc.
Can anyone help me please :)
You could create two Custom Dimensions, each for Type and another for Country
As per your need define the appropriate Scope of the dimension, a Hit level or Session level scope would be appropriate.
You need to push custom dimensions into Google Analytics i.e. additonal JS code in your site.
ga('send', 'pageview', {
'dimension1': 'Magzine',
'dimension2': 'Singapore'
});
How this works
User scans the code and visits the store
Site has a JS code snippet that would get the query parameters from the URL and sets a custom dimension for each parameter
Setting the custom dimension would let Google Analytics know the value of the Type and Country
It is your JS code that tells Google Analytics what value to take for custom dimension. Google Analytics would not know that the value came from the URL.
To get a query parameter value via javascript you can refer to this answer, If you take the function provided there by Jan Turon (head over and give him an upvote of this helps you):
function getJsonFromUrl() {
var query = location.search.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
}
You can use this to dynamically set the dimensions based on the url. You first call the function to return an JSON object that has the key/value pairs from the query parameters, then you insert the needed values to set the dimensions:
result = getJsonFromUrl();
ga('send', 'pageview', {
'dimension1': result.qrcode_type,
'dimension2': result.location
});

Google Analytics: Change userID at runtime in a SPA

The documentation indicates that the userId must be set this way:
ga('create', 'UA-XXXX-Y', { 'userId': 'USER_ID' });
But in a Single Page Application (SPA), the user starts as anonymous and then logs in. So the app will start with:
ga('create', 'UA-XXXX-Y', 'auto');
And when he logs in, then I would like to change to a specific ID for tracking that user, but when I try:
ga('create', 'UA-XXXX-Y', { 'userId': 'USER_ID' });
Nothing happens, the user ID does not appears in subsequent requests.
Which is the right way of setting the userId at runtime?
Thanks.
Unfortunately, the documentation is currently incorrect. It is possible to set the user ID outside of the create method.
The reason your example isn't working is because you're calling create twice. What you want to do is call set. Here's how:
// Create the tracker instance.
ga('create', 'UA-XXXX-Y', 'auto');
// Once you know the user ID, set it on the current tracker.
ga('set', { userId: USER_ID });
Now all subsequent hits sent to GA will be associated with this user ID.
UPDATE:
The user ID documentation now reflects that it can be set outside of the create method.

Individual User ID with Universal Analytics

I am attempting to implement a very basic UserID parameter, so that I can more easily slice the data we are receiving (ie attribute User to device, returning/non-returning, location, source, event)
I am using this at the moment to create unique IDs for each visit:
var timedate = new Date();
var timedatemil = timedate.getTime();
ga('create', 'UA-XXXXXXXXX', { 'userId': timedatemil});
To produce a simple ID for each visit. However I am not receiving any data this way when I switch to the userID view. Why would this be? I am running a simple landing page so have no identifiable system to tie it to currently.

Resources