How to populate google analytics UTM variables manually - google-analytics

I want to be able to set the UTM variables manually
So instead of having a webpage
http://mysite.net/index.html?utm_source=source&utm_medium=inbound&utm_campaign=campname
I want to be able to set these with javascript. Looking through the documentation I couldn't find any set methods for these, only the set key methods.
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCampaigns
I have also tried
_gaq.push(['_trackPageview', '/index.html?utm_source=source&utm_medium=inbound&utm_campaign=campname']);
Although as far as I can tell this doesn't work. The only way I can now see of getting this to work will be to set these using a hash value:
ie:
http://mysite.net/index.html#utm_source=In+House&utm_medium=email&utm_campaign=Fall+email+offers
_gaq.push(['_setAllowAnchor', true]);
Is there a better way?

edit
Actually it seems there IS a way to do this using GA code!
previous answer
I've needed to do this in the past as well and I could not find any way to do it on-page. I believe the only way you can really do this is by reading GA's __utmz cookie and rewriting the cookie with the value(s) you want.
example:
This is what __utmz would normally look like on if you go to www.mysite.com with no url params (the numbers in the cookie will be different):
URL: http://www.mysite.com
__utmz cookie value: 97566023.1329384140.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Here is what it would look like if you were to go to the URL with the values in the URL (the official way to do it):
URL: http://mysite.net/index.html?utm_source=In+House&utm_medium=email&utm_campaign=Fall+email+offers
__utmz cookie value: 97566023.1329384140.1.1.utmcsr=In House|utmccn=Fall email offers|utmcmd=email
And the cookie will have these values on subsequent page views, and you will see them sent to GA in the utmcc URL param in the request URL. So basically you can alter the __utmz cookie to the values you want. Would probably be more convenient to write a wrapper function to easily set it. Honestly, I don't know why GA doesn't provide a way to do this with a baked in wrapper function...

Related

LinkedIn sharing SHARE_URL#HASH URL Param

I am faced with a LinkedIn sharing issue.
This issue probably reproducible from March 1st 2019.
I share some url e.g. https://www.linkedin.com/sharing/share-offsite/?url=SHARE_URL#HASH
Worked before: link in post(href) - SHARE_URL#HASH
Works now: link in post(href) - value of og:url meta tag from SHARE_URL#HASH page
So we lose request parameters in SHARE_URL and #HASH
How we can pass link for LinkedIn post into request?
You need to do URL-encoding with parameters you are feeding to another URL. So, this is what you should want...
https://www.linkedin.com/sharing/share-offsite/?url=SHARE_URL%23HASH
Remember, URL's use things like ? and # to indicate a special argument occurring after this character. So, for instance, example.com/share.php?title=thisistitleright?&..., how would the browser know that the first ? indicates the GET param and the second ? is a part of the title argument? Easy: URL encoding.
In case you want to know more: Official LinkedIn Share Documentation

Will Google Analytics treat url parameter as part of URL?

I need to incorporate my own parameters as part of url. (e.g. #student=DD&start=Date1&end=Date2). However, I am cerncerning about if I add my part to the url, will it mess up my google analytics tracking? Basically, now my url is something like -- MywebsiteUrl?userID=AAA#student=DD&start=Date1&end=Date2&utm_source=CC....
What url Google analytics will track? is my website url -- MywebsiteUrl, or ``MywebsiteUrl?userID=AAA#student=DD&start=Date1&end=Date2`?
Thanks in advance!
Google Analytics will treat query parameters as part of the URL. However it will not treat #student=DD&start=Date1&end=Date2 as having query paramaters. As far as (native) Javascript and the GA tracking code is concerned the URL ends after the fragment identifier (#).
So your complete example
MywebsiteUrl?userID=AAA#student=DD&start=Date1&end=Date2&utm_source=CC
you will get MywebsiteUrl?userID=AAA. If the userID parameter contains personally identifiable information you need to remove it to conform to Google's terms of service. You can use a filter or the exclude parameters-box in the view settings.

Getting accurate referrals from arbitrary sites analytics.js

Working on a web app where at step 2 the user leaves to her domain and comes back. The domain she leaves to is dynamically entered into our form (this step is required for an OAuth token flow).
So we're having that domain appear as a referrer and want to keep the original referrer in Google Analytics.
Used to be (with ga.js) you'd just pass utm_nooverride=1 in the callback URL coming back to your site, but we're using analytics.js.
Linker Params won't work because we can't dynamically control the callback URL.
So: Questions:
1) Does the excluded referrers list in GA for a web property allow regular expressions? It allowed *.sample.com to be saved, but I haven't tested whether it works or not yet.
2) Or is there an API call to our GA account we can make to add dynamic excluded referrer domains on the fly?
Any other way to solve this?
Long time no seen, funny to find you here.
Now for your question, I see 2 options,
The Referrer exclusion list doesn't support regexes. But it does exclude all subdomains. So if all the domains you are interested in removing are subdomains of something specifically you can remove only the top level domain and should be good. More Info
There's a trick you can use to force analytics.js to not send the referrer to GA if you don't want to. You can manually set the referrer to an empty string on that page. You can do it always or when it matches the user entered hostname. It would look something like this:
var hostname_to_ignore = 'site.com';
ga('create', 'UA-XXXXX-Y', 'auto');
if(document.referrer.indexOf(hostname_to_ignore) > 0) {
ga('set', 'referrer', '');
}
ga('send', 'pageview');
Not sending the referrer will keep GA from creating a new session and it should just continue the previous session if the last hit was less than 30 min ago. That's probably what you want. I didn't test this, it's possible that setting it to null or undefined is even better, I would try it out and check what the hits look like.
More Info

how to create this nonce?

I need a bit of help understanding what this means:
https://wordpress.org/plugins/json-api/other_notes/#Method:-create_post
Required argument
nonce - available from the get_nonce method (call with vars controller=posts >and method=create_post)
How should I create that nonce?
I'm trying:
wp_nonce_field( 'posts', 'create_post' );
Note that you don't have to use the wordpress nonce functionality directly, instead use Json Api's given method in the post controller.
You may try the solution of bbottema
First you need a nonce (Number used ONCE), which is a temporary key you'll use to be able to invoke the API with:
http://yourblog.com/?json=core.get_nonce&controller=posts&method=create_post&callback=?
This gives you a nonce number (be sure to use &callback=? as it marks the content as jsonp, or you'll get a similar -but invalid- nonce).
Then use this nonce to create a post:
http://yourblog.com/?json=posts.create_post&nonce='+nonce+'&title='+title+'&content='+content+'&status=publish (or draft, or leave it out altogether)
Make sure you have the 'posts' controller enabled in your wordpress plugin JSon API settings. Check this manual for what JSon data structures you can expect back from these calls.
Now, here's the tricky part: you need to be already logged into the wordpress site, because with this JSon API, you can't log in. I haven't figured that part out yet, so I'm still looking for a good solution myself. I tried manually posting and also width ajax but with limited results considering I'm missing a WordPress test cookie in my headers (at least this is the main difference I see when logging in from the site and doing it manually)
JSON API USER Does not work, it will only work if you are logged in to the website with an account of ADMIN ROLE!.. otherwise it won't create valid nonce

Google Analytics: _linkbypost() on non-form cross domain links?

I'm just curious is it possible (or advisable) to use _linkbypost() instead of just _link() on cross domain links with Google Analytics to avoid the problems I'm having with the long query strings that _link() produces.
_link() uses _GET to pass data by attaching a huge gibberish query string to the destination url which causes me a few headaches: It prevents my caching scheme (which keys off exact matching urls), drives many of my social media widgets crazy (which have proven super important to my business), and just looks scary and ugly which I've found really does affect how much many users trust your site.
So I'm hoping I can get the same ability to track without losing my clean orderly cacheable urls by passing that data via post instead of get. But since I don't really understand how post works I don't know if this if feasible, or if it is just a really bad idea for some other reason.
I know _linkbypost() needs a form object to function, so my plan was to add an onSubmit function to each cross-domain link like so:
var crossLink = $(this).attr("href");
var formHTML = '<form id="crossForm" action="'+crossLink+'" method="post"></form>';
$('body').append(formHTML);
var crossForm = $('#crossForm');
_gaq.push(['_linkByPost', crossForm]);
return false;
Assuming it's not a bad idea to begin with, does that implementation seem reasonable?
I'm pretty sure _linkByPost will still sen the data through your url. So I don't think that's a solution to your problem.
You can use _link to pass the query parameters at the anchor (instead as query parameters) part of the url using it's second argument as true.
_gaq.push(['_link', 'http://www.myothersite.com', true]);
This will generate a url like
http://www.myothersite.com#__utma=1.2.123123...
You will also need _gaq.push(['_setAllowAnchor', true]); to tell GA to read the data from the Anchor.
It should be enough to not break your cache anymore and reduce the issue with your social plugins.

Resources