Search:

Welcome

Login to contribute. Register if you haven't yet.

Need Support?

Search this site to find an answer. If you still need help, post a Question. Private or account-related support inquiries can be made at the Private Support forum.

Stay Informed

Are you receiving EditMe's monthly newsletter? Keep informed of product changes, tutorials and tips. Sign up here.

Segmenting Google Analytics data

Forums / Questions

jezcope says:

I have got Google Analytics installed, but would like to segment visitors according to whether they are registered users or not (using the _setVar function described here). What's the best way to do this using server-side JavaScript?

On a related note, I'd also like to factor out the GA code into a separate page which can then be included by the main layout page, but I get this error. What might be the problem here?

Thanks,
Jez

Posted at 12:56 AM on September 1, 2009


matt staff says:

Jez, Yes, integrating user tracking is a great idea. I'll update the post with these details tomorrow. Your included page shouldn't be running as an EditMe Scripted page. Just copy the code from Google as is into a normal text/html page. Cheers, Matt

Posted at 4:37 AM on September 1, 2009

matt staff says:

Jez,

I've set this up on this site to see if it works. Once I see it showing up in the reports, I'll update the tip to include this useful modification.

Here's what I have pasted at the end of the Layout for this site. Bolded parts are what was added to track the current user name.

I'm not sure what the delay is for tracked data to be shown in the reports - I'll check it tomorrow and hopefully I'll see something. Let me know if it works for you.

<script editme="eval var uname=user.isLoggedIn?user.name:'Anonymous';; parse">
var uname='{uname}';
</script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("YOUR TRACKER CODE HERE");
pageTracker._setVar(uname);
pageTracker._trackPageview();
} catch(err) {}</script>

Posted at 10:34 AM on September 2, 2009

jezcope says:

Thanks Matt, that looks great. I'll let you know if it works. One note: Google's terms of service prohibit collecting "personally identifiable information of Internet users", so I don't think one would be allowed to track specific usernames. Only logged-in vs. anonymous or user groups would be permissible.

If I put this code in a separate page to be included by the layout, how can I make sure that the server-side bit at the top is evaluated correctly? Will the page be evaluated on include, even if its MIME-type is text/html?

Posted at 1:14 AM on September 2, 2009

matt staff says:

Interesting. That's of questionable use for most EditMe customers, then. Though does that apply if a user has "opted in" by registering and logging in on your site?

RE the include: No, you'd have to make the page a Scripted Page. Scripted pages have to be valid XML, which anything that already works in a Layout page should already be.

Posted at 2:05 AM on September 2, 2009

jezcope says:

Since we're using EditMe to collaboratively contribute to a publicly accessible site rather than to collaborate on private projects, it was exactly the distinction I was trying to make. Thanks to your help I've now got it working properly. My GoogleAnalytics page contains this code:

<div>
<!-- Google Analytics Javascript code -->
<script type="text/javascript" editme="eval var loggedin=user.isLoggedIn;; parse">
var loggedin={loggedin};
</script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
// <![CDATA[

try {
var pageTracker = _gat._getTracker("UA-10236534-1");

// Extra code to track Google Image Search as a search engine
// See http://sn.im/riuw3 for information
var ref = document.referrer;
if (ref.search(/images.google/) != -1 && ref.search(/prev/) != -1) {
var regex = new RegExp("images.google.([^\/]+).*&prev=([^&]+)");
var match = regex.exec(ref);
pageTracker._clearOrganic();
pageTracker._addOrganic("images.google."+ match[1],"q");
pageTracker._setReferrerOverride("http://images.google." + match[1] + unescape(match[2]));
}

if (loggedin)
pageTracker._setVar('LoggedIn');

pageTracker._trackPageview();
} catch(err) {}
// ]]>
</script>
<!-- end Google Analytics -->
</div>

which is then included in the main layout with

<div editme="process 'GoogleAnalytics'" />

The problem I was having with scripted pages was related to needing to be a valid XML document, which must have exactly one root element: this code has three script elements at the 'root' level. I got around this bp wrapping everything in a <div> tag: not ideal but it works. Should scripted pages instead be required only to be valid XML rather than being valid XML documents in their own right?

On an unrelated note, code pasted here from the EditMe code editor seems to have some strange formatting applied, and this editor doesn't have a button to remove the strikethrough format.

Posted at 6:18 AM on September 5, 2009

matt staff says:

Jez,

Good to hear you got this working.

Valid XML has to have a root tag, so if a root tag were not required, it wouldn't be valid.

That said, you can use the SS Self-Removing Tag at the root if you don't want the root tag published.

Matt

Posted at 3:10 PM on September 5, 2009

You must log in to reply

EditMe News & Blog