How do I dynamically pass the current domain name from CGI-scope to google analytics javascript?

47 views
Skip to first unread message

Thorsten Eilers

unread,
May 22, 2013, 1:49:10 PM5/22/13
to cfwh...@googlegroups.com
Hi,

how would you do this?

In the main layout I include the google analytics javascript like this.

#javaScriptIncludeTag("google-analytics.js")#<!--- Google-Analytics Code --->

The analytics code is like this:

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-12345']);
 _gaq.push(['_setDomainName', '#CGI.server_name#']);    // TODO: How to dynamically pass the current domain?
 _gaq.push(['_trackPageview']);
 _gaq.push(['_trackPageLoadTime']);
 
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

How do I dynamically pass the current domain to the javascript?
In the moment it is shown like #CGI.server_name# in the browser, but it should be like mydomain.com

Any help is appreciated
Thorsten

Andy Bellenie

unread,
May 22, 2013, 2:03:20 PM5/22/13
to ColdFusion on Wheels
<cfoutput>?



Thorsten

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cfwheels+u...@googlegroups.com.
To post to this group, send email to cfwh...@googlegroups.com.
Visit this group at http://groups.google.com/group/cfwheels?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tom King

unread,
May 22, 2013, 4:55:13 PM5/22/13
to cfwh...@googlegroups.com
Wot Andy said. Outputting CF values in a javascript config is perfectly allowable.
Just remember a user could change those values (hypothetically) so don't use it for anything major important.

Thorsten Eilers

unread,
May 23, 2013, 3:19:16 AM5/23/13
to cfwh...@googlegroups.com
Well, cfoutput in the javascript file included via javaScriptIncludeTag does not work.
Coldfusion does not process the .js file like a .cfm file.
As a workaround I moved the javascript to the layout.cfm file.

Is there no way to have coldfusion process a file with .js filetype? Or would this be bad style?

Tom King

unread,
May 23, 2013, 4:19:07 AM5/23/13
to cfwh...@googlegroups.com
ColdFusion isn't going to process a file with a non .cfm extension (unless you set up the webserver to specifically do that - hypothetically it's possible to make say, .html run through the cf parser) but I really really wouldn't do that.

Ultimately, .cfm = serverside, .js = clientside. If you want the javascript file to have access to cf variables, you need to do it either by cfoutputting them on the serverside, so the resultant javascript has them 'hard coded' in, or you can use a .cfm to return the javascript, either in your layout file, or include a separate one. Lastly, you could create ajax methods to return the data from cf in JSON and do it that way.

Andy Bellenie

unread,
May 23, 2013, 7:28:37 AM5/23/13
to ColdFusion on Wheels
You can't do that in a JS file. Take the code and place it at the bottom of your main layout page inside a <script> tag instead.


--

George

unread,
May 23, 2013, 1:03:14 PM5/23/13
to cfwh...@googlegroups.com
You CAN do that with a js file. Like Tom said, you can do it with HTML or anything really. I have a CF server, serving .JPG request. Now, is that best practice and recommended. NO. Would I do it today, NO. But You should not tell him that you can't do it. It is better to say that it is bad practice as it creates confusion on the server side and also because in reality the extension is usually irrelevant to the client as long as your markup and response headers are correct. You could easily have <script src="mysite.com/script.CFM"></script>

However, in the end Wheels gives you really pretty ways to do this. using their conventions you can dynamically create your scripts with .js.cfm extensions. 

HOWEVER, having said all this. The Google analytics script best practice is to have it injected directly into your page. Their are many reasons having do to with page load time calculations and network stuff. 

Thorsten Eilers

unread,
May 28, 2013, 2:49:27 AM5/28/13
to cfwh...@googlegroups.com
Thanks for your input. I will include the analytics code snippet in the layout file. Or just use a plain cfinclude to have it in the different layout files of my app.

Reply all
Reply to author
Forward
0 new messages