Setting variable and using HttpSubModule

I’m trying to insert google analytics code into an app by using the
directions at Module ngx_http_sub_module.

In so doing, I first tried to set a variable as follows:

set $analytics = “var _gaq = _gaq || []; _gaq.push([‘_setAccount’,
‘UA-xxxxxx-xx’]); _gaq.push([‘_trackPageview’]); (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); })();”;

to get the text into the variable but of course that generates an error:

nginx: [emerg] invalid number of arguments in “set” directive in
/usr/local/etc/nginx/sites-enabled/mysite.com:12.

Searching Google I found a few threads about trying to place the
contents of a file into a variable but there didn’t seem to be a way.

I also tried simply adding the text into the “sub_filter” line but that
also threw an error:

nginx: [emerg] unexpected “_” in
/usr/local/etc/nginx/sites-enabled/mysite.com:16

Using the following:

server {

sub_filter
‘’;
sub_filter_once on;


}

resulted in the following:

jim$ curl http://mysite.com/somepage

...

Putting the path in quotes changed nothing other than that portion being
in quotes.

Obviously I’m doing something wrong. Any help would be appreciated.

Jim O.

Hello!

On Sat, Jun 25, 2011 at 12:39:19PM -0400, Jim O. wrote:

= document.getElementsByTagName(‘script’)[0];
s.parentNode.insertBefore(ga, s); })();";

to get the text into the variable but of course that generates an error:

nginx: [emerg] invalid number of arguments in “set” directive in
/usr/local/etc/nginx/sites-enabled/mysite.com:12.

Syntax is

set $variable "value";

Using “=” is a syntax error.

http://wiki.nginx.org/HttpRewriteModule#set

Searching Google I found a few threads about trying to place the
contents of a file into a variable but there didn’t seem to be a way.

I also tried simply adding the text into the “sub_filter” line but that
also threw an error:

nginx: [emerg] unexpected “_” in
/usr/local/etc/nginx/sites-enabled/mysite.com:16

Probably some other syntax error. Please make sure you’ve quoted
text properly.

Somthing like this works fine here:

sub_filter “”
"";


Putting the path in quotes changed nothing other than that portion being
in quotes.

There result is exactly as configured, it’s not clear what you expect
here.

Maxim D.

On 6/25/11 3:59 PM, Maxim D. wrote:

‘UA-xxxxxx-xx’]); _gaq.push([‘_trackPageview’]); (function() { var

Syntax is

set $variable "value";

Using “=” is a syntax error.

Module ngx_http_rewrite_module

Thanks Maxim. My goof. That solved it.

Searching Google I found a few threads about trying to place the
contents of a file into a variable but there didn’t seem to be a way.

[…]


Putting the path in quotes changed nothing other than that portion being
in quotes.

There result is exactly as configured, it’s not clear what you expect here.

That’s what I found out. :stuck_out_tongue: I did some digging before I asked for help
here and I found
http://www.lowendtalk.com/questions/7846/automatically-reference-ga-codes-from-external-file-using-nginx-substitution-module
which had that configuration and was obviously wrong. However that is
where I got the idea to at try it.

Again, thanks for the help.


Jim O.

Hello!

On Sat, Jun 25, 2011 at 05:06:23PM -0400, Jim O. wrote:

[…]

}
in quotes.

There result is exactly as configured, it’s not clear what you expect here.

That’s what I found out. :stuck_out_tongue: I did some digging before I asked for help
here and I found

http://www.lowendtalk.com/questions/7846/automatically-reference-ga-codes-from-external-file-using-nginx-substitution-module

which had that configuration and was obviously wrong. However that is
where I got the idea to at try it.

It’s not “wrong”, it just references GA code in an external
javascript file which will be loaded by browser as a separate
resource. There is some downsides of this aproach (browser will
have to load another javascript resource), but it’s not wrong.

Maxim D.