You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Neil Ferguson <nf...@gmail.com> on 2009/07/04 11:53:26 UTC

Re: Trying to add string literal to Javascript for Google Analytics

FYI, I solved this by putting my Javascript in a separate file, and
loading this using a PackagedTextTemplate. The code for my footer
panel looks like the following:

public FooterPanel(String id, String analyticsPagename) {
		super(id);
		PackagedTextTemplate template = new PackagedTextTemplate(getClass(),
"ga-execute.js", "text/javascript");
		Map variables = new HashMap();
		variables.put("analyticspagename", analyticsPagename);
		TextTemplate interpolated = template.interpolate(variables);
		Label label = new Label("googleanalytics", interpolated.asString());
		label.setRenderBodyOnly(true);
		label.setEscapeModelStrings(false);
		add(label);
}

And ga-execute.js looks like:

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview('${analyticspagename}');
} catch(err) {}</script>

And the markup for my footer panel is very simple:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
>
	<body>
	    <wicket:panel>
	        <wicket:container wicket:id="googleanalytics"></wicket:container>
		</wicket:panel>
	</body>
</html>

Thanks to everyone for all your help.

Neil.

On Tue, Jun 23, 2009 at 12:14 PM, Neil Ferguson<nf...@gmail.com> wrote:
> Hi.
>
> Thanks for the reply. I did read that blog post previously, but I
> don't really want the pageTracker._trackPageview function to be run in
> the page header. For Google Analytics (or any other Javascript-based
> analytics for that matter) the best practice is to put the tracking
> code just before the close of the body tag, to ensure that it's run
> only when the page is fully loaded. Perhaps I'm missing something
> though, would it be possible to do this with the
> TextTemplateHeaderContributor?
>
> Thanks.
>
> Neil.
>
> On Mon, Jun 22, 2009 at 7:08 PM, Juan Carlos Garcia
> M.<jc...@gmail.com> wrote:
>>
>> Have you Try using a
>> http://wicket.apache.org/docs/wicket-1.3.2/wicket/apidocs/org/apache/wicket/util/template/TextTemplateHeaderContributor.html
>> HeaderContribution , factoring out that JavaScript into a separate .js  and
>> then do variable Interpolation.
>>
>> See the following example at
>> http://chillenious.wordpress.com/2006/05/03/wicket-header-contributions-with-behaviors/
>> Eelco Hillenius blog's
>>
>> Hope this is what you need.
>>
>>
>> Neil Ferguson-2 wrote:
>>>
>>> Thanks for the reply. I'd rather keep the script in my markup if
>>> possible. Is there any way to do this so that I don't have to output
>>> the entire Javascript block from my Java code?
>>>
>>> Thanks.
>>>
>>> Neil.
>>>
>>> On Mon, Jun 22, 2009 at 6:24 AM, Igor Vaynberg<ig...@gmail.com>
>>> wrote:
>>>> output the entire javascript block as a string using new
>>>> label().setescapemodelstrings(false)
>>>>
>>>> -igor
>>>>
>>>> On Sun, Jun 21, 2009 at 11:18 AM, Neil Ferguson<nf...@gmail.com>
>>>> wrote:
>>>>> Hi all.
>>>>>
>>>>> I'm trying to set a string literal in some Javascript, in order to try
>>>>> and pass a page URL to Google Analytics. I've tried creating a panel
>>>>> with the following markup, and putting it just before the end of my
>>>>> body tag:
>>>>>
>>>>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
>>>>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
>>>>> <html xmlns="http://www.w3.org/1999/xhtml"
>>>>> xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd"
>>>>>>
>>>>>        <body>
>>>>>            <wicket:panel>
>>>>>                <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("TRACKINGID");
>>>>>                                pageTracker._trackPageview('');
>>>>>                                } catch(err) {}
>>>>>                    </script>
>>>>>                </wicket:panel>
>>>>>        </body>
>>>>> </html>
>>>>>
>>>>> As you can see, I'm trying to set the parameter of the _trackPageview
>>>>> method (or function, whatever it's called in Javascript). I've tried
>>>>> doing this using a span tag, as you can see, but it doesn't work, and
>>>>> I get the following message:
>>>>>
>>>>> "WicketMessage: The component(s) below failed to render. A common
>>>>> problem is that you have added a component in code but forgot to
>>>>> reference it in the markup (thus the component will never be
>>>>> rendered)."
>>>>>
>>>>> The Java code for my panel looks like the following:
>>>>>
>>>>> public class FooterPanel extends Panel {
>>>>>
>>>>>        private static final long serialVersionUID =
>>>>> 9196070061210793618L;
>>>>>
>>>>>        public FooterPanel(String id, String analyticsPagename) {
>>>>>                super(id);
>>>>>                Label label = new Label("analyticspagename",
>>>>> analyticsPagename);
>>>>>                label.setRenderBodyOnly(true);
>>>>>                add(label);
>>>>>        }
>>>>>
>>>>> }
>>>>>
>>>>> I didn't really expect this to work, but I can't think of a better
>>>>> way. I'm sure that I'm missing something really obvious here, so can
>>>>> anyone help?
>>>>>
>>>>> BTW, I'm really enjoying using Wicket. I have a severe allergy to XML,
>>>>> so it's brilliant.
>>>>>
>>>>> Thanks.
>>>>>
>>>>> Neil.
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>>
>>
>> --
>> View this message in context: http://www.nabble.com/Trying-to-add-string-literal-to-Javascript-for-Google-Analytics-tp24137280p24152508.html
>> Sent from the Wicket - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org