Google Analytics - How to find URL of the previous page in jQuery? - javascript

I am trying to distinguish where people are coming to my website on a specific page. So if they come from my list of set pages, I want to pass a different URL to Google Analytics. If the user directly types up the link, the previous page would not be amongst the et pages I have in my list. Hence those visits I will pass a different URL to GA.
I know how to pass pageviews in GA. My problem is, how do I get to know what page they were on beore. I tried using document.referrer, window.location.hostname, and all that jazz but nothing seems to work.
Need help soon, thanks!

You can do that with PHP only (or any other server-side language) generally. The referrer is sent with the HEAD request to the server, so, in PHP you would use
$_SERVER['HTTP_REFERER']

Related

Can I programmatically get the traffic channel for the current page in javascript google analytics?

I want to programmatically know the traffic channel for the current page using javascript google analytics (or reporting) API. Like organic, google search, direct, referral, etc.
I can get referral with
tracker.get('refferer')
but I need to know the traffic channel.
For example channels
Can I get it? And if I can, then how?
no, usually you can not access it since channels are based upon source and medium combinations and these fields are processed on the server side based on referrer, document.location and previous visits information. Google Analytics provides a chart of the processing flow on the help pages
Of course, you can replicate some of the processing on the client side, by parsing utm labels from the location.href or checking the referrer but 1) this would require a ton of scripting 2) this wouldn't necessary match Google Analytics data
Some workaround can be to use classic analytics ga.js snippet alongside your usual analytics code with any dummy property ID. in this case ga.js code would process much of the source data on the client site and these data would be available from the utm* cookies
The source / medium information is available only on the landing page, if you want to have it available on a subsequent page you need to store it in a cookie. Here's an example how to do it: https://www.youtube.com/watch?v=x2pR4bCW7Mg
At list you can get from which URL you were visited:
Javascript -> document.referrer
PHP -> $_SERVER['HTTP_REFERER']
ASP -> Request.ServerVariables("HTTP_REFERER")

Standard practice for tracking a redirect page?

How can I add google analytics tracking code to a page that redirects.
For example say I have a vanity url www.simpleurl.com that is redirected to www.hardtorememberlongurlofdoom.com/folder/?q=qwerqwe
What would the best method be to track hits to the simpleurl.com. Currently Im using the redirect feature from my hosting company. What I think I should do is turn that off and create and index.html that runs the tracking JS and then redirects the page.
Does anyone know if there is a standard practice for this technique?
If you want to track it with JavaScript and not by using PHP on the simple domain (before the redirect) then you could set the script to track on the long domain and filter by referrer? You can get the referrer using document.referrer.
Another solution could be to set up Google Analytics to track by referrer on all visits and then you've got data to see how many goes through the (or maybe even several) simple domain(s) and how many goes straight to the long domain.
EDIT:
Actually you could just have the tracker on the long domain and all the data should be fine in Google Analytics: Here
The only problem with this approach would be if you want information about the referrer from the simple domain.

Authenticate websites that are using my service through iframe

I have a service that I provide to different websites. For the website to use the service they implement a javascript that triggers a lightbox with a iframe in it.
The problem is I don't want any website to be able to use the service/iframe. Is there any way I can authenticate the websites using the iframe?
The way I see it, a random website can just take the javascript from a website using the service/iframe and implement it on it's own website.
Authenticating the actual users inside the iframe is no problem, there will be a login form and a register form. It's just the websites using the iframe I want to identify and authenticate.
Any suggestions is appriciated!
You could check document.referrer in JavaScript, and if the referring domain is not within a whitelist you provide (or the referrer is empty), don't display content at all.
But we all know how unreliable the referrer is, and it might exclude users even under wrong conditions.
Another way would only work it the sites embedding your iframe have server-side scripting and so can calculate some kind of hash - f.e. the hash of the current date and a secret keyword - and pass it as a GET parameter in your iframe URL. In your script, you check if the given value is the same as the hash you create with the same data - and display content based on that or not.
Using the current date could be problematic though if time zone settings for your server and the other site are different, and it might also break when a user calls the page around midnight. To prevent such problems, you could have the remote sites include the unix timestamp value used to create the hash as GET parameter as well - then you can check if that timestamp is not to old, and create the hash with that exact value. (Then other sites might try to just copy the parameters and use them on their page as well, but it will only work for a small time window.)

Same url for app, pagetab and freestanding site in authorized facebook-app?

I have a facebook app that requiers authorization, therefore I have to pass a redirect url along with the authentification-request.
My app is entierly front-end code.
If I wan't the standalone-version, I can just pass the window.location.href
Something like this won't work for the app-page, because it's iframes from different servers. Therefore I have to hardcode the app-url in this case.
The app is intended to be embedded on different app-pages, so I can't hardcode that. Apperntly I can get the facebook page-tab-link etc using the facebook api. Haven't looked in to the details yet though. Redirect back to page tab after user authenticates?
As facebook doesn't allow parameters, my plan is to add /pagetab/ or /app/ to the end of my url, and add that to the various urls in the settings of facebook. After that I will create different cases for the different url-endings.
Although this soultion sounds like a lot of work. Is there a better way to do this?
If your app wasn’t totally client-side, then you could find the info which page your app is embedded on in the signed_request parameter. But since Facebook POSTs that to the iframe on calling your app, there is no way of accessing it client-side.
As facebook doesn't allow parameters
Facebook allows for a parameter called app_data in the URL, which is passed on to your app. But again, this happens via POST on first page load.
If you were willing (and able to, regarding your platform) to make the little adjustment of having your apps HTML code generated by a server-side language (instead of it being purely static HTML pages), then you could easily evaluate the signed_request parameter, and have the page id written into a JavaScript variable, so that you can use it client-side from there.

Google Analytics not tracking clientside redirects

I'm trying to solve a problem with tracking traffic to individual blog posts on a system that uses a javascript redirect system where something somehow break tracking.
The way the system works is as follows:
Permalinks are in the format of domain.com/ab_XXXXXX where ab_XXXXXX is a unique code. That code is parsed by a piece of javascript, which tells the browser to load the individual blog post page. This page has a URL format of blog.html/?cd_XXXXXX_ef=YYYYYY. Here too, XXXXXX and YYYYYY are unique codes which, combined, identify the particular page.
When I load the blog.html/?cd_XXXXXX_ef=YYYYYY URL directly, Google Analytics does seem to track the page and the user shows up in the real-time dashboard as visiting that particular page.
However, when I load the domain.com/ab_XXXXXX URL, the user does not show up in Analytics' real time dashboard, even though the analytics tracking gif gets loaded.
Since the ab_XXXXXX URL is the permalink, Analytics appears to contain no data on individual blog post visits at all.
I'm not the programmer on this system, and at this point I'm not sure what to test or to suggest as changes to the programmers.
My question is: why would Analytics track the user in the one occasion, but not in the other? And what could be done to remedy this?
Does the page ab_XXXXXX actually redirect the user to cd_XXXXXX_ef=YYYYYY or does it simply load the contents of that blog post into the same page? In either case, but particularly the latter, you can simply choose to manually track the page view in your existing Javascript snipped by adding something along the lines of _gaq.push(['_trackPageview', 'blog.html/?cd_XXXXXX_ef=YYYYYY']);

Categories