I hope this is not too off-topic. I am interested in how to get site search and google analytics to work.
I tried porting over analytics.html and I get the following error:
executing "partials/google/analytics.html" at <.google.analytics>: can't evaluate field analytics in type interface {} in partials/google/analytics.html
I would’ve tried site search first but your commit message said you only have it “sort of” working.
I have a bunch of logic in partials/google/analytics.html to prevent it from displaying if the config.toml isn’t set up. That’s through a couple of variables in the config.toml file. Could that be what’s causing your error?
The part that isn’t really working is the search. I have a Javascript hack that forces the sitename into the Google search variable when it is posting. It actually works okay for what it does.
Thank you for pointing out the analytics page present in the hugo documentation. I will be looking into it.
Actually, the above error happens when I do have ga_tracking enabled. But I actually had the account variable set up with empty quotes. That might actually have caused the error but I won’t know for sure until I get a chance to test it.
and in layout/partias/piwik/anaylitics there is following JS snippet:
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u = "//" + {{ $piwik_url }} + "/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', {{$site_number}}]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="//piwik.domain.tld/piwik.php?idsite={{$site_number}}" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
The value of {{ $site_number }} template variable is referenced as intended, but, being total JS noob, can’t understand why the string concatenation in the line
var u = "//" + {{ $piwik_url }} + "/";
does not happen?
I’ve tried to change double quotes ("") into single ones (’’), as well as using concat() function, but same result.
Anyone can give some clue why the variable ‘u’ is not assigned the concatenation of the 3 strings:
“//”
value of $piwik_url variable which is declared as:
{{ $piwik_url := .piwik.piwik_url }}
within the template and
“/”
Of course, I’m open for any more elegant solution as well.
I think that it is at the wrong level. {{ $xyz}} is substituted in the Go template. That substitution is exact - it doesn’t include the quotation marks. Try
I think that it is at the wrong level. {{ $xyz}} is substituted in the Go template. That substitution is exact - it doesn’t include the quotation marks. Try
var u = "//{{ $piwiki_url }}/";
Thanks a lot. That works!!!..but still have to think a little bit to
grasp it fully.
One thing that will help is is time spent looking through errors. You’ll eventually get to where you recall seeing the exact same thing before.
Another thing that will help is to look at the output. Your output was something like:
var u = "//" + piwik.domain.tld + "/";
Couple of ways to fix that. My preference (obviously) was to put the template variable into the JS string directly. Another would be to change the definition to match the usage:
[Params.piwik]
piwik_url = "'piwik.domain.tld'"
That would result in:
var u = "//" + 'piwik.domain.tld' + "/";
Sometimes that will be the desired solution. The reason I prefer the first in this case is to avoid a needless concatenation at run time. Now if your script had something like:
var pTLD = {{ $piwk_url }};
var u = "//" + pTLD + "/";
One thing that will help is is time spent looking through
errors. You’ll eventually get to where you recall seeing the exact
same thing before.
Thanks,
Couple of ways to fix that. My preference (obviously) was to put the
template variable into the JS string directly.
But, isn’t hardcoding string into JS bad practice and make it
invonvenient for other users wanting to use Piwik and make it
configurable via Huho’s config file?
Another would be to change the definition to match the usage:
[Params.piwik]
piwik_url = "'piwik.domain.tld'"
Do you find that’s acceptable option for Hugo’s config?
The reason I prefer the first in this case is to avoid a needless
concatenation at run time.
I understand, but there is trade off to be made in configurability vs
performance.
Now if your script had something like:
var pTLD = {{ $piwk_url }};
var u = "//" + pTLD + "/";
Then you’d need the tics in your definition.
Well, I consider your original workaround more elegant.
Are you interested to include Piwik support in your theme?
All the options use a value from the config file to create the output and the concatenation in this snippet isn’t really going to impact performance. You have a nicely named variable so it’s very readable any which way you choose to proceed.
Please go ahead and submit a pull request when you’re done and I can add it.