nystudio107 / craft-instantanalytics

DEPRECATED: Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.
https://nystudio107.com/
Other
20 stars 9 forks source link

setDocumentTitle not working: always overriden by template name/path #39

Closed bencresty closed 3 years ago

bencresty commented 4 years ago

Describe the bug

To my surprise the plugin sends the template name/path with a pageView and it doesn't seem to be possible to override this title page/document title for the pageView event. I tried setDocumentTitle() as seen in the documentation and done it exactly like the examples show for use in Twig, but it doesn't work.

I would expect this to work as that's what the documentation says:

{% set myAnalytics = craft.instantAnalytics.analytics() %}
{% do myAnalytics.setDocumentTitle('my title here') %}
{% hook 'iaSendPageView' %}

Which sends the pageview, but doesn't change the title.

I also tried this:

{% set myAnalytics = craft.instantAnalytics.analytics() %}
{% do myAnalytics.setDocumentTitle('test titel').sendPageView() %}

But like this (with .sendPageView() ), but without the 'iaSendPageView' hook, the pageview doesn't get send.

But neither of those send the event with the given title.

typo in documentation BTW It didn't really help there is a typo in the documentation on this; sendPageView is written with lowercase v in the documentation, but everywhere else it's written with a capital, also I see it written with a capital V in the code. (Btw I tried both, but neither don't seem to actually send the pageview from Twig, only using the hook seems to actually send) image

Looking in the code I see this: getGlobals-always-set-template-as-title and this 2-sendPageview-always-overwrite-titel-when-seomatic

So it looks to me that there's no way to override the title from the template ourselves as this seems to always be overriden by the templates name/path in the sendPageView() method. And also after that it would also even override that title when seoMatic is used (I'm not using seoMatic btw and don't need it either as I already have that functionality build).

Unfortunately for me and customers it's a must to be able to set the title before being able to use this plugin.

To me this is such a no brainer that developers would like to be able to set their titles to something other than the template names, and looking at the plugins you wrote and the quality you deliver I'm starting to wonder if I'm missing something here. Please tell me if I'm missing something here. I read the documents over and over, looked in the code and tried many things, but none seem to work to actually set the title. Well, without changing the plugins code that is and it's obvious I don't want to do that.

If I'm not missing something, hopefully this could get fixed soon so we can use it on a new site that's now waiting for this.

Thanks in advance!

To reproduce

Steps to reproduce the behaviour:

  1. Use twig code as above
  2. Go to a page
  3. Look in analytics

Expected behaviour

See above

Screenshots

See above

Versions

khalwat commented 4 years ago

I realize it may look like a typo in the documentation, but the lowercase v is actually correct on the internal Analytics object that is being called to send the data. I did test it, and it does send analytics data if done like this:

{% do instantAnalytics.setDocumentTitle('woof') %}
{% do instantAnalytics.setDocumentPath('woof/bark') %}
{% do instantAnalytics.sendPageview() %}

When done via the hook method, you can still override the document title and document path:

{% do instantAnalytics.setDocumentTitle('woof') %}
{% do instantAnalytics.setDocumentPath('woof/bark') %}
{% hook 'iaSendPageView' %}

Results in this:

        'dp' => TheIconic\Tracking\GoogleAnalytics\Parameters\ContentInformation\DocumentPath#9
        (
            [*:name] => 'dp'
            [*:value] => 'woof/bark'
            [TheIconic\Tracking\GoogleAnalytics\Parameters\SingleParameter:indexPlaceholder] => ':i:'
        )
        'dt' => TheIconic\Tracking\GoogleAnalytics\Parameters\ContentInformation\DocumentTitle#10
        (
            [*:name] => 'dt'
            [*:value] => 'woof'
            [TheIconic\Tracking\GoogleAnalytics\Parameters\SingleParameter:indexPlaceholder] => ':i:'
        )

Which is the expected result, we've changed both the documentTitle and the documentPath -- it looks like it's going to overwrite the existing settings, but the method just returns the already created Analytics object (with any changes your template has made to it) here:

    public function getGlobals($title)
    {
        if ($this->cachedAnalytics) {
            $analytics = $this->cachedAnalytics;
        } else {
            $analytics = $this->pageViewAnalytics('', $title);
            $this->cachedAnalytics = $analytics;
        }

        return $analytics;
    }
khalwat commented 4 years ago

However, what will cause it to overwrite the documentTitle is if you have SEOmatic installed and you use the automatic or hook method (documentPath will not be overwritten in this case).

So you can either send the page view directly yourself with:

{% do instantAnalytics.sendPageview() %}

Or if you like, I can add an option to not set the documentTitle automatically if SEOmatic is installed. Let me know!