nystudio107 / craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
https://nystudio107.com/plugins/seomatic
Other
166 stars 71 forks source link

Google gtag.js script did not render #1158

Closed KarlBishop closed 2 years ago

KarlBishop commented 2 years ago

I've added an AdWords Conversion ID and switched on Google gtag.js Enabled on various sites in a multisite Craft instance.

When I look at the source code of a page, I see <script>/* Google gtag.js script did not render */</script> in the head.

I had a look at previous related issues #942 and #944 but didn't find a way to fix my problem yet:

Any ideas on what might be causing this or how I could troubleshoot?

Versions

Screenshot

seomatic_tracking_gtag_issue

khalwat commented 2 years ago

@KarlBishop can you check your web.log? There should be more detail there on exactly why it couldn't render that template.

You can try diagnosing it also by temporarily replacing the Body Script Template with a simple Twig expression or the like, to see if that renders.

On the surface, I don't see anything wrong with it however... and I'm able to enable gtag locally here without issue.

KarlBishop commented 2 years ago

I checked my web.log and there weren't any errors.

Tried outputting simple Twig in the Script Template and Body Script Template and this worked fine.

Then did some testing of the default code and found my issue was caused because gtagProperty was evaluating as blank in the first line of both templates.

Fixed the problem by replacing this:

{% set gtagProperty = googleAnalyticsId.value ?? googleAdWordsId.value ?? dcFloodlightId.value ?? null %}

With this:

{% set gtagProperty = googleAnalyticsId.value ??? googleAdWordsId.value ??? dcFloodlightId.value ??? null %}
khalwat commented 2 years ago

Interesting. So those values are not null, they are empty strings?

KarlBishop commented 2 years ago

Seems so, yes.

E.g. if I add the following in the Script Template:

{% if googleAnalyticsId.value is defined %}
    console.log("googleAnalyticsId.value is defined");

    {% if googleAnalyticsId.value is null %}
        console.log("googleAnalyticsId.value is null");
    {% else %}
        console.log("googleAnalyticsId.value is NOT null");
    {% endif %}

    {% if googleAnalyticsId.value is empty %}
        console.log("googleAnalyticsId.value is empty");
    {% else %}
        console.log("googleAnalyticsId.value is NOT empty");
    {% endif %}

    {% if googleAnalyticsId.value == "" %}
        console.log("googleAnalyticsId.value is an empty string");
    {% else %}
        console.log("googleAnalyticsId.value is NOT an empty string");
    {% endif %}

{% else %}
    console.log("googleAnalyticsId.value is NOT defined");
{% endif %}

Then the console output is:

googleAnalyticsId.value is defined
googleAnalyticsId.value is NOT null
googleAnalyticsId.value is empty
googleAnalyticsId.value is an empty string
khalwat commented 2 years ago

So it looks like this was already fixed a bit over a year ago:

https://github.com/nystudio107/craft-seomatic/blob/develop/src/templates/_frontend/scripts/gtagBody.twig

https://github.com/nystudio107/craft-seomatic/blob/develop/src/templates/_frontend/scripts/gtagHead.twig

Committed on Mar 31, 2021: https://github.com/nystudio107/craft-seomatic/commit/83dea919197f6464e214436bd20e28b3d509361f

It doesn't auto-update the scripts, because it's possible you might have changed them, and we don't want to blow away any changes you've made.

KarlBishop commented 2 years ago

Ahh ok, in that case I find this is a bit unintuitive - I wouldn't have guessed that my template code was over a year out-of-date because I only created the site 4 months ago and had only just enabled the Google gtag.js settings last week.

My expectation would be that, if I'm only just activating the template editor box for the first time now, then it would be using the current default.

I'm guessing my issue was caused because:

I might add a feature request for ways to avoid this type of confusion (even if only for myself).

khalwat commented 2 years ago

Yeah the defaults are in place as soon as SEOmatic is installed. Flipping the lightswitch on just shows you the settings you can edit.

The only way around it that I can think of would be a checksum on the template code, and if they match, we can safely update. Unfortunately this doesn't handle the case of where you make a minor change to the template code... because then it wouldn't update it.

The best thing is obviously to not make any errors to begin with (which is really hard to do given the complexity of the system). After that, I think probably it's best to have a consistent mental model that anything the user can edit in the CP won't be updated by future updates.

I'm open to discussion on that though!