Jump to content

Google Adwords Conversion Tracking broken


Recommended Posts

Hi all,

 

this issue is related to javascript compression and moving the javascript to the end:

 

I cannot track conversion via Adwords, because "Error: No HTTP response detected".

 

When I check the source code, I see that tracking code is displaced and divided into multiple pieces.

 

Furthermore, when I disable moving the javacsript to the end, the whole javascript on the page does not load :/

 

Any help?

 

I am ready to send all the necessary data you will need.

 

SOLVED:

 

By adding the code:

 

{if isset($js_defer) && !$js_defer && isset($js_files) && isset($js_def)}
    {$js_def}
    {foreach from=$js_files item=js_uri}
    <script type="text/javascript" src="{$js_uri|escape:'html':'UTF-8'}"></script>
    {/foreach}
{/if}

 

To the header.tpl and by disabling moving the javascript to the end.

 

Thanks to El Patron.

Edited by VajdaShark (see edit history)
Link to comment
Share on other sites

  • 2 months later...
  • 5 weeks later...

Did anyone find a solution for this? I have tried adding the solution proposed by VajdaShark in header.tpl (just before the {$HOOK_HEADER}) but it is till not working.

 

The tag assistant still displays: No HTTP response detected

 

Any Help would be appreciated!!!

Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...

Thank You

 

This may help you : https://gist.github.com/prestarocket/1ca849e576fb439183e3
 
After override the class Media with this code, you just have to write your tracking code with the data-keepinline attribute :
 

<script type="text/javascript" data-keepinline="true"></script>

Thank You

I have simplifyed it a bit
 

Class Media extends MediaCore
{
	public static function deferScript($matches)
	{
		if (!is_array($matches))
			return false;
		$inline = '';

		if (isset($matches[0]))
			$original = trim($matches[0]);

		if (isset($matches[1]))
			$inline = trim($matches[1]);
		// prestarocket
		/* This is an inline script, add its content to inline scripts stack then remove it from content */
		if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
			return '';
		//end prestarocket
		/* This is an external script, if it already belongs to js_files then remove it from content */
		return parent::deferScript($matches);
	}
}

But I still have Issue, even though i added Media file and added data-keepinline="true" and deleted class index file and refreshed cache.

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...

I don't understand, you have replaced everything which is by default in Media.php with that small code and there are not damages in some other places?...

its called to override and you do it by creating the same file in the override folder (read about it in the docs) - and actually as you can see Im "extending" in a way so that if my scrpt dont do anything, it will just do as the parent (default) function normally do.

Edited by fransjaeger (see edit history)
Link to comment
Share on other sites

Hi again this is how i do it, and this works with minifying JS and HTML and Moving JS/CSS to bottom.

 

Im showing the scripts inline and not moving them or changing them, if i just do the  data-keepinline="true" trick

 

override/classes/Media.php

Class Media extends MediaCore
{
	public static function deferScript($matches)
	{
		if (!is_array($matches))
			return false;
		$inline = '';

		if (isset($matches[0]))
			$original = trim($matches[0]);

		if (isset($matches[1]))
			$inline = trim($matches[1]);
		// prestarocket
		// This is an inline script, add its content to inline scripts stack then remove it from content
		if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
			return '';
		//end prestarocket
		// This is an external script, if it already belongs to js_files then remove it from content
		preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
		if (array_key_exists(1, $results))
		{
			if (substr($results[1], 0, 2) == '//')
			{
				$protocol_link = Tools::getCurrentUrlProtocolPrefix();
				$results[1] = $protocol_link.ltrim($results[1], '/');
			}
			if (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src))
				return '';
		}

		// return original string because no match was found
		return $original;
	}

	public static function packJSinHTML($html_content)
	{
		if (strlen($html_content) > 0)
		{
			// This is an inline script, add its content to inline scripts stack then remove it from content
			if (preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $html_content) == 0)
				return str_replace(array('>/*  */', '/*  */</script>'), array('>/* <![CDATA[ */', ' /* ]]> */</script>'), $html_content);
			return parent::packJSinHTML($html_content);
		}
		return false;
	}
}

Part of my footer.tpl

<script data-keepinline="true" type='text/javascript'>
	var google_tag_params = { 
		ecomm_prodid: '{$product->id}{if isset($combinations) && $combinations}-1{/if}-{$lang_iso}',
		ecomm_pagetype: 'product',
		ecomm_totalvalue: '{$product->getPrice(true, $smarty.const.NULL, $priceDisplayPrecision)|floatval}'
	} ;
</script>

to make ganalytics module work correctly I had to also edit that fil in modules/ganalytics/ganalytics.php

	private function _getGoogleAnalyticsTag($back_office = false)
	{
			// added a line -> ga(\'require\', \'displayfeatures\');
			// added  data-keepinline="true"
			/* original code 
			return '
			<script type="text/javascript">
				(window.gaDevIds=window.gaDevIds||[]).push(\'d6YPbH\');
				(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
				(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
				m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
				})(window,document,\'script\',\'//www.google-analytics.com/analytics.js\',\'ga\');
				ga(\'create\', \''.Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')).'\', \'auto\');
				ga(\'require\', \'displayfeatures\');
				ga(\'require\', \'ec\');
				'.($back_office ? 'ga(\'set\', \'nonInteraction\', true);' : '').'
			</script>';
			*/
			return '
			<script type="text/javascript" data-keepinline="true">
				(window.gaDevIds=window.gaDevIds||[]).push(\'d6YPbH\');
				(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){
				(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
				m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
				})(window,document,\'script\',\'//www.google-analytics.com/analytics.js\',\'ga\');
				ga(\'create\', \''.Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')).'\', \'auto\');
				ga(\'require\', \'ec\');
				'.($back_office ? 'ga(\'set\', \'nonInteraction\', true);' : '').'
			</script>';
	}
Link to comment
Share on other sites

  • 8 months later...
×
×
  • Create New...