Search
Other Sourcerer questions
Forum

Sourcerer inserted a script tag inside another script tag

Greg Dorr's Avatar Greg Dorr
Hi Peter, I'm using Sourcerer to insert both a Google Analytics tag and another script into the HEAD section of a site. After a few days and they weren't tracking, I looked at the source code of the page in my browser and noticed that both scripts were inserted by Sourcerer inside another <script> tag before its closing </script>. I'm trying to narrow down the source of the other script to see if I can disable it or move it.
Confidential information:
(hidden)


Thanks,
Greg
Peter van Westen's Avatar Peter van Westen ADMIN
Well, Sourcerer pretty much just does what you tell it to.
What code have you placed inside the {source} tags?
Please post a rating at the Joomla! Extensions Directory
Greg Dorr's Avatar Greg Dorr
I created a custom module and clicked the "Code" button at the bottom of the editor. The Sourcerer dialog opened up and I clicked the Javascript tab, chose Placement > HTML Head and pasted my GA4 code into the Code field. The result:
{source}<?php
$doc->addScriptDeclaration('
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());

gtag(\'config\', \'G-XXXXXXXX\');
</script>
');
?>{/source}
Peter van Westen's Avatar Peter van Westen ADMIN
Well, then Sourcerer is working fine.
Your code is incorrect.
The 'addScriptDeclaration' function already creates a <script> tag. You are only supposed to pass the javascript part itself to it.

Please read: docs4.regularlabs.com/sourcerer/the-basi...ipt-scripts-or-files
And: docs.joomla.org/Adding_JavaScript
Please post a rating at the Joomla! Extensions Directory
Greg Dorr's Avatar Greg Dorr
Maybe a note to this effect -- "do not include script tags" -- in the app would be helpful. It seems counterintuitive to me to not include the script tags. Maybe an option to put straight code into the <head> rather than only Javascript?

What would be your recommended method of inserting something common like Google Analytics into the head with Sourcerer? GA code includes two script tags and comments. Does it need to be rewritten or added in pieces if using Sourcerer?
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());

gtag(\'config\', \'G-XXXXXXXX\');
</script>

Thanks!
Peter van Westen's Avatar Peter van Westen ADMIN
Joomla doesn't provide a method to add whole blocks of HTML blocks to the head of the site.
If you have a javascript file tag and an inline script tag, you'll need to add them separately.

See: docs4.regularlabs.com/sourcerer/the-basi...ipt-scripts-or-files
And: docs.joomla.org/Adding_JavaScript

Or check if your template has a 'Custom Code' thing in which you can place the full code to be inserted at the bottom of your HTML page.
Personally, I recommend that approach.
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription