Get Source and Medium from ga.js - google-analytics

I have a newsletter page on my website which I'd like to also use to capture some additional information about the user using Google Analytics.
Using the getAll() method from ga.js I can get the referrer URL (if any), however I cannot see the medium or source data. Is this possible to retrieve using their ga.js library?

Retrieve specific tracker from the list of trackers you obtained.
Use the following to access the fields source and medium.
tracker.get('campaignSource') and tracker.get('campaignMedium')
Additional help from below links.
Access data from ga object:
https://developers.google.com/analytics/devguides/collection/analyticsjs/accessing-trackers
Field reference:
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference

This is not possible with ga.js.
However, you can use custom JavaScript to deduce the same information.
If you only want this capability, then I suggest you have a look at the library I wrote: https://github.com/ilkkapeltola/visitdata
Once the library is included, you can run visitData.get(), which returns an object with exactly the data you need.

If you are using google analytics core reporting api try this
'metrics': 'ga:sessions ,ga:pageviews',
'dimensions' : 'ga:source,ga:medium'

Related

How do I pass a parameter from my site into google analytics?

Let's say I have a site and the user comes into it with a parameter:
http://example.com&url=blahblahblah
How do I go about passing along the url value from the parameter into Google Analytics?
1) User comes to the page with a url in the params
2) User clicks a download link with a ga tracking code attached to it which was generated from ga account like this:
http://example.com/download/param1=dkljdf&_ga=1.149898996.39207121.1424368466
You have to create a custom Dimension and a metric for that.
About custom Dimensions and metrics:
https://developers.google.com/analytics/devguides/platform/customdimsmets
After you have created a Dimension, you can add metrics to it by view, in example.
Follow the steps here for Universal Analytics:
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
Note that due to not have 10 point of reputation, I wrote the a "_" in http like "ht_tp"
BUT:
I think what you want to know is the number os visitors that clicks a download link in your site that comes from, lets say "blahblahblah" as web origin or other methods.
For that, you have the param utm_source that you can receive directly in the url.
So instead of ht_tp://example.com&url=origin you should receive ht_tp://example.com&utm_source=origin
In this way, you have no care about it. Analytics is going to take care for you so you can get a report of clicks by source.
Or, just use the referer in case all the incoming visitors are from webs:
ga('set', 'referrer', 'ht_tp://example.com');
And a final option, to use Events:
_gaq.push(['_trackEvent', 'ReferencedVisitors', jsVarWhereYouHaveTheOrigin]);

How to Track Store Locator Search terms in Google Universal Analytics

I am in the process of setting up enhanced Ecommerce for GA and have had a few requests of additional of what else the client would like to see in their reports. Once of which is
"Where can i see a report if the locations that a use types in when they use the store locator"
There is already an internal search functionality thats been set up and is tracking the search terms people are typing when looking for products, but i'm not sure if i would need to set this up as a second search terms report or if it's something different?
The URL of the page is different to the internal search results and is www.domain.com/store-locator#wales|GB|0|0|0
Any insight into this would be really helpful.
Thanks,
Roxi
As to me knowledge, you are not able to use bookmarks (#something) in GA for internal site search setup. Only GET and POST parameters are allowed. In your situation I think the best solution is to use GA events to send the data about used location each time the user is using this functionality. You need to include addition ga() function call to track those events. Info about how to set it up you could find here: event tracking. After setting things up, you will see all the info about number and type of called events in Behavior -> Events reports section in GA.
Example code:
<button onlick="var hash_location=window.location.hash;ga('send','event','Locator',has_location)">Click me</button>
With such function new event will be send to GA with Event Category=Locator and Event Action=hash in the url. You have some complicated hash, so most probably you need to extract first some info from this using regular expression. Example to get first item from |-separated list in the hash:
var pattern = new RegExp('[^#|]+');
var hash_location = pattern.exec(window.location.hash)[0];

Integrate optimizely into google analytics

I have been trying to integrate optimizely into google analytics and it has proved a bit difficult than I anticipated. I am doing it through gtm which makes it slightly complicated. I have created a macro
function() {
dimension = optimizely.allExperiments[experimentId].universal_analytics.slot;
experimentIds = optimizely.activeExperiments;
variationName = optimizely.variationNamesMap;
experimentDetails = variationName + "|" + experimentIds;
return experimentDetails;
}
and joined it to a custom dimension in universal analytics tag which means this will be fired on every page but the dimension always comes back as:
'Running command: ga("set", "dimension14", undefined)'
I will really some help on this as I am struggling and the optimizely team dont know what to do either.
Thanks.
While Optimizely is best installed directly onto your page, it is possible to install Optimizely using Google Tag Manager (GTM), which is discussed here, with a complete walk through. This will ensure Optimizely fires before GA (or any other tag):
https://help.optimizely.com/hc/en-us/articles/200040095-Implementing-the-Optimizely-Snippet#tag_manager
The next part is troublesome - how can you integrate Optimizely with Google Analytics? To do so, you must insert a line of code before the GA tracking call is made ( ga('send','pageview') ). To do this, you must modify the JS in the GA call to look like the code below.
Please note, unless GTM allows you to modify their GA add on, you will need to add a custom integration in GTM for GA to place the modified code.
// Optimizely Universal Analytics Integration code
window.optimizely = window.optimizely || [];
window.optimizely.push(['activateUniversalAnalytics']);
ga('send', 'pageview');
The 'activateUniversalAnalytics' call is similar to the macro you've written above, where all it does is pass your experiment ID# (or name) and the variation # (or name) to the custom Dimension you've assigned to GA in Optimizely. One benefit of using Optimizely's integration is you can change the Dimension number per experiment inside Optimizely without making any changes to your page's source code.
If you need further assistance with this, please contact support#optimizely.com (full disclosure, I am an Optimizely employee).

Accessing ClientID set by Google Analytics through Google Tag Manager

I have setup a new Universal Analytics tracking and would like to store ClientID in one of the custom dimensions slots.
The tracking tags are implemented through Google Tag Manager (using the predefined UA tag) and I am having the difficulty to passing the Client ID (via custom JavaScript Macro) with the basic pageview request.
I have named my tracker so that I can access it and this code runs without any error in console, however when used with GTM, it's returning an error.
ga.getByName('trackerGTM').get('clientId'))
produces an error:
Uncaught ReferenceError: ga is not defined
Any tips?
I assume you're firing the pageview {{url}} matches RegEx .*, correct? Could be that Universal hasn't loaded yet. Have you tried {{event}} equals gtm.dom? I haven't tested this, but it's a thought.
EDIT:
This looks like a bug. I was able to run:
function(){
var client = ga.getAll()[0].get('clientId');
return client;
}
And have it spit out the client ID with no problem.
The real problem is that GTM (Google Tag Manager) creates an object that is NOT called ga (it creates an object named ga followed by a random number), that's why 'ga' is not defined. You should try something on the lines of using the tag manager to put that client id into a javascript variable so you can access it from the page)
I was reading up on setting client ids and came across this blog link about accessing client ID in GA while using Google Tag Manager. Might be helpful.
Accessing Client ID in GA using custom dimensions

How to modify page URL in Google Analytics

How can you modify the URL for the current page that gets passed to Google Analytics?
(I need to strip the extensions from certain pages because for different cases a page can be requested with or without it and GA sees this as two different pages.)
For example, if the page URL is http://mysite/cake/ilikecake.html, how can I pass to google analytics http://mysite/cake/ilikecake instead?
I can strip the extension fine, I just can't figure out how to pass the URL I want to Google Analytics. I've tried this, but the stats in the Google Analytics console don't show any page views:
pageTracker._trackPageview('cake/ilikecake');
Thanks,
Mike
You could edit the GA profile and add custom filters ...
Create a 'Search and Replace' custom filter, setting the filter field to 'Request URI' and using something like:
Search String: (.*ilikecake\.)html$
Replace String: $1
(was \1)
Two possibilities come to mind:
it can take a while, up to about 24 hours, for visits to be reflected in the Analytics statistics. How long ago did you make your change?
try beginning the pathname with a "/", so
pageTracker._trackPageview('/cake/ilikecake');
and then wait a bit, as per the first item.
Usually you have the ga script code at the end of your file, while special _trackPageviews() calls are often used somewhere else.
Have you made sure you have your call to pageTracker._trackPageview() after you have defined the pagetracker?
Like this:
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview();
otherwise you just get a JavaScript error I suppose.

Resources