Can I set GA custom variables anywhere in the page? - google-analytics

I'm just getting started with splittesting using Google Analytics (GA). Rather then using different URLs for different versions of a page I want to use the same URL and differentiate the versions via GA custom variables.
I've managed to accomplish this in my Django project with a conditional in the view-layer that loads up different html templates for different versions and the html-templates also take care of setting the custom variables.
However I think that it would be a lot cleaner to have all of my splittest code in a javscript file that:
1) Sets/reads a cookie on the browser specifying which testing bin this browser is in.
2) Modifies the page html and css to create the different versions
3) Records the version in a GA custom variable
Right now I'm setting all of my custom variables right in the code snippet that I've copy-pasted from GA.
My question is simply: Can I set the custom variables later in the page buy a _gaq.push(), or do they need to happen before the anonymous function that ends the snippet from GA?

Custom variable data gets recorded when GA makes a tracking GIF request, which occurs with _trackPageview or _trackEvent.
So, you can set the custom variables anywhere in the page, as long as there's a _trackPageview or _trackEvent somewhere afterwards.

Related

Access GTM google tag manager variables from outside of GTM?

I was reading about GDPR and GTM here https://www.analyticsmania.com/post/gdpr-cookie-consent-notification-with-google-tag-manager/
basically how to make GTM gdpr valid.
the issue I saw, was that I don't know how to read any GTM variables from outside of GTM. E.g. in that guide, they store a variable or function that will figure if consent was given or not. How can I read, utilize functions and react to events from outside of GTM?
say I run my little javascript file myapp.js on the page. In there I want to for example not add youtube video embeds in case of they have not given consent. Or add videos and share links if they gave consent. This is not something I can do via GTM obviously.
Well the dataLayer variable is public so you can read it at any time it just might be a little hard to find an specific value since you need to move though an array.
If you want to use a javascript variable on your site as a GTM variable then the variable needs to have global and not local scope (the dataLayer is global but you need to perform a dataLayer.push() to add items to the array).
If the javascript variable on the site is responsible for other site functions and has global scope, then either reference it using a GTM javascript variable or custom javascript variable and use it to act as the trigger exception.
The alternative is to have the site logic prevent the GTM snippet from loading. In the GDPR perfect world, you would have granular opt-out depending on the tags in GTM. You could create a form on site with opt-outs for specific tags, however, that data needs to be stored per customer and delivered to the dataLayer to perform tag specific opt-out functionality.
Can you expand on the logic you are attempting to reference from the site?

Google Analytics Event track on page load

I'm trying to track pageviews on certain types of pages across multiple websites with different url structures (which rules out using a RegEx to segment a common section of the URLs). Is it possible to track page loads with an event script? Would it be used as an onLoad event?
You CAN utilize regex to accomplish this, though it does depend on how many rules you have.
Example if you want to include these two urls:
www.google.com/inventory/example
www.microsoft.com/vehicle/example2
you can use
.*(\/inventory\/|\/vehicle\/).*
Another option is to use Google Data Studio and create a calculated field based on the "page" dimension and filter/group using CASE function.

Why do my Google Analytics URLs begin with slashes?

When looking at Google Analytics, all reports show URLs that begin with a slash and www: "/www.url.com/page.html."
I've never seen Google report like this. Webmaster Tools is set up correctly. Not sure what else can be set up in Analytics. Any idea?
Current: /www.url.com/page.html
Typical: /page.html
by default GA only reports the relative path and query string of the URL (it strips the protocol and domain. So one of two things must be happening:
1) you have code that is passing a custom page name to the _trackPageview call, adding that "/[domain]".
2) you have a custom filter within the interface setup that is prefixing the page name with "/[domain]"
Adding the domain to the page name is a fairly common thing to do when you have GA code spanning multiple domains, most especially when they are going to a rollup profile, so that you can see which pages are coming from which domain.
So if I had to guess (and this is only a guess, seeing as how I don't have access to your
code or GA interface), someone must have attempted to rebuild the full url to use as the page name instead of just the path+querystring - and then messed up (probably a messup in some regex with the protocol, if I wanted to throw even more guesses at it).
But the 64 thousand dollar question is.. where is it being changed? Like I said.. GA by default does not do this, so someone has added code to do it on your site, or else a filter within the interface.
I would start by looking to see if there are any filters in your interface, since that is the easier thing to determine. If you see no filters relevant to this, then you will have to look on your page code (including any script includes or other javascript code being output). It would be a value passed with _trackPageview so ctrl+f for that.

Google Analytics Content Experiments: Possible to setup Variations for Multiple Pages at once?

I've recently learned about the new Google Analytics Content Experiments which looks interesting. ( http://analytics.blogspot.nl/2012/06/helping-to-create-better-websites.html )
The standard usecase seems to be that for a certain page, say a product detail page, you supply variations (different urls) and select a percentage of users that are included in the test. Such a user will be presented a variation of the product-detail page (and will continue to be presented the same variation over and over for continuation/ux reasons, based on cookies presumably) .
All fine and good.
However, say I have 100 products on my site. Just testing a variation on 1 of those products has imho the following disadvantages:
slow progressing tests because of lower nr of visitors.
the test isn't isolated. I.e: since other product detail pages aren't included in the test, displaying a variation-page for 1 product-detail page while all other product-detail pages show the original can (will) lead to a confusing experience (and thus skewed conversion statistics) for the user that browses multiple products, which most of them do.
To me it seems far better to be able to dynamically include all products of a certain type into the same test (e.g: all TV's) , for example by enabling to set some regular expression or other filter on urls to include in the test.
Is such a thing possible currently, scheduled, useful, or completely missing the point?
EDIT
Part of the solution seems to be "relative urls"
https://support.google.com/analytics/bin/answer.py?hl=en&answer=2664470
Taking the previous example one step further, we can see how the use
of relative URLs lets you easily run an experiment on a set of
different original pages, and test visual alternatives across that
group of pages (e.g., the product pages in an e-commerce site).
Remaining question: How to dynamically tag which pages belong to the experiment (e.g: based on regex)
Thanks.
The solution is to use relative url for the variation page.
E.g. you have a number of product pages:
www.mysite.com/products/eggs.html
www.mysite.com/products/cheese.html
www.mysite.com/products/bread.html
etc.
For each page you have a matching variation page:
www.mysite.com/products/eggs.html?var=bigpicture
www.mysite.com/products/cheese.html?var=bigpicture
www.mysite.com/products/bread.html?var=bigpicture
etc.
You want to use all the product pages in 1 experiment.
Go To google Analytics Content Experiments:
For the orginal page choose ONE of the many product pages (e.g. www.mysite.com/products/eggs.html) (This is just to get the experiment code and provide GA with an example page)
For the variation page choose relative url and put ?var=bigpicture
Then place the javascript required for the experiment on ALL the original product pages you want in the experiment
For more information see: http://support.google.com/analytics/bin/answer.py?hl=en&answer=2664470&topic=1745208&ctx=topic
Use the Javascript API as described here:
https://developers.google.com/analytics/devguides/collection/gajs/experiments#pro-server
You can set the experimentid programmatically in your code, on every page. Of course you need first to create the experiment in GA, in doing so provide GA fake urls for each variation, discard the GA generated code, ignore the validation errors.
And just use the experimentid as described in the link above.
OK, so a solution to this is:
Create experiment.
Chose a placeholder url for your original url. Something like www.example.com/products/eggs. Set variations as relative urls eg ?var=large_heading, ?var=small_price
Have some mechanism on the server-side which determines if the current user is part of the experiment. A simple cookie is good enough. If this cookie is present show a variation of the page.
If the user visits a product page but isn't in an experiment then show the javascript given when you created an experiment.
Add something to your product page which checks for the querystring var=[something]. When detected show the appropriate variation as well as setting the cookie which tells marks the user as being in an experiment.
You can hack around the JavaScript that Google gives you to make this a bit easier. Something like:
var variation = utmx('variation_code', 'A/B');
if (variation) { set_a_cookie(variation); }
utmx('url', 'A/B');
This is largely cribbed from the GWO Techie Guide. http://static.googleusercontent.com/external_content/untrusted_dlcp/www.google.com/en//websiteoptimizer/techieguide.pdf
There is also a way to the A/B testing with GA without experiment API if you really want to keep things simple. The idea behind it is to create your own split parameter and than you can pass it to GA as a custom variable. So you can yous your own development tools to differentiate the content in the groups and you don't have to use redirect. Here is a simple tutorial how to do this: link.
I recently implemented a GA experiment to test out different text on a nav bar across many pages. This is what worked for me:
Set up the experiment in GA for a single page. E.g. index.html and
index.html?var=menu2.
Implement the solution across multiple pages. Specifically,
insert the GA experiment code in all the pages
that you want to run
the test. Then ensure that your page(s) can render the page
variation based on the parameter passed. My php code went something
like this: If var=menu2, display page with menu2; otherwise,
display original menu.

Why does Google Analytics cross-domain tracking across an iframe require tracking snippets in both domains?

I'm creating a shareable widget, for anyone to copy onto their website. The shareable piece of code is an frame that points to the actual widget that lives on our hosted site (i.e. ourdomain.com). If we ever want to tweak the actual widget, we can do so in one place, with no effect on the iframes pointing to it from other sites we have no control over.
I tried the approach of cross domain tracking. While I was analyzing the results, I observed that the path of the shared widget code (not on our domain) appears within the content view in GA. This runs counter to a response from my previous post "if it is being inserted into many domains you are going to need to set up multiple GA accounts and use different account numbers per user."
Correct me if I am wrong: Any tracking code using our unique account id will appear in our GA by default, no matter where it's hosted, whether the code is implementing cross domain tracking or not.
http://www.google.com/support/forum/p/Google+Analytics/thread?tid=6af5b4c3e30c71be&hl=en
Since any page that hosts GA tracking code shows up in our content view, I could locate where the widget is being viewed by simply looking for all pages whose name did not include ourdomain.com. Of course this includes proxy servers as well, but I was going to also include a custom variable so I could implement a filter based on the custom variable.
So explain to me where this breaks down or what I'm missing here. Is there some amount of data I am losing here because the GA cookie is not in fact being utilized? Give me a good reason why I should put in the effort to fully implement cross domain tracking, including the necessary P3P implementation on our server for IE visitors.
As a sidenote, I am considering adding GA tracking code with an event tracker within the widget itself to track when people actually USE the widget (as distinct from the when the widget is loaded). I understand that by not implementing cross domain tracking I would not be able to obtain any data about the visitor, only whether the widget was acted upon or not.
As the referring site is the primary bit of information we need,
I'm going to pass the referrer in the URL that loads the iframe content
and then overide the referrer within the tracking code
_gaq.push(['_setReferrerOverride',ht_referrer]);
as documented here:
http://www.prusak.com/google-analytics-referrer-override/
This way, I don't need to inject tracking code into my widget, just some JavaScript that adds the referrer to the URL that loads the iframe.
Wish me luck.

Resources