Integrate optimizely into google analytics - 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).

Related

Server Side Test A/B + GTM

I just created my server side A/B test following this https://developers.google.com/optimize/devguides/experiments;
But i dont have GA tracker in my code anymore since we are using Google Tag Manager.
How to replace this ga('set', 'exp', '$experimentId.$variationId'); using GTM?
Thanks!
In GA -> Variable (GA Settings) or into your PageView Tag you can add expId and ExpVariant. Really hard to find a solution for this but it makes sense now.

Setting up Adobe Analytics eCommerce tracking in GTM

I am currently in the process of setting up Adobe Analytics on a website that is currently using GTM & GA (universal analytics). Ideally I want to leverage the existing data layer to pass values to the Adobe evars/props but the one question I have was how to handle the products field? Adobe requires the products field to be in a very specific format (category;product;qty;total).
Can I build this product string in GTM? Or do I need the developers to hard code this string? I might be overthinking this but I have never installed Adobe thru GTM before
You can use a custom javascript variable (GTM "variables" used to be called "macros" which was a lot less confusing) of the type "custom javascript function" to loop through the products array and assemble the product string.
That is you'd first create a dataLayer variable that returns the product array from the dataLayer and then do a loop; a custom javascript variable in GTM is an anonymous function that returns a value. This would look a bit like that (from top of my head, not exact code; the thing in curly brackets is the datalayer products variable you need to create first):
function() {
var products = {{products}};
var productAdobe = [];
for(i=0;i<products.length;i++) {
prod = products[i];
productAdobe.push(prod[i].category + ";" + prod[i].id + ";" + prod[i].quantity + ";" + prod[i].quantity*prod[i].price);
}
return productAdobe.join("\n");
}
Like I said this is not exactly working code, but should be enough to give you an idea.
Having said that having your developers pass on a proper product string is a much better idea. Using GTM for programming tasks is error prone and makes maintenance hard (and tag management should make your life easier, not harder). And if you already have Adobe Analytics you should have free Access to Adobe DTM which might a better choice to deploy Adobe Analytics (for one thing you cannot host s_code within GTM, as the custom HTML tag has a length limit that is exceeded by the Adobe Tracking code).

What is the correct syntax to set a custom dimension in Google Analytics?

I'd like to send some custom dimensions back to GA with every blog post that is read on my website - author and category.
When I set up the custom dimensions in GA I got this code to use:
var dimensionValue = 'SOME_DIMENSION_VALUE';
ga('set', 'dimension2', dimensionValue);
However, in the GA docs it specifies a different syntax using "send" rather than "set".
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
ga('send', 'pageview', { 'dimension4': '<?=$categories?>'});
We are using universal google analytics, but this conflicting information means I'm not sure which syntax to use.
Thanks in advance
The first code block is how you set the custom dimension (CD) that can be sent with any hit: event, pageview, transaction, etc.
The second code block is how you set the CD and send it with a pageview. So it's a specific example of the first method.
Both are valid, it's just that the second example is more complete.
When sending data to CDs, don't forget to create and define them in the GA configuration as well.

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

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

Resources