If we get traffic from a site which sends a utm_source param along with a link for a product then if some customer buys using that link we need to put a code on the Thank You page of Order completion.
What code would be added to the checkout.post.php to send the data to that pixel for traffic coming with that specific utm param.
Can some one please guide us.
You can just add HTML block with the required code to the Order landing layout page:
http://prntscr.com/9hyymi
Or you can use a checkout:order_confirmation hook.
3rd variant is to use Custom HTML code option in the layout page settings
http://prntscr.com/9iayc9
The issue is not the location for the code but how to track if the user is coming from a link with a specific utm_source only. Since when user browses around different pages the utm_source param is lost since its not posted across pages.
How do we track it, can we use a cookie to set when a user lands on any page on the website and then check the cookie while the order completes. If this can be done then where do i add the code so that always the source is checked and a cookie is set even if the user come on any page site wide.
OK. You can store it in the session. PHP code:
if (!empty($_REQUEST['utm_source'])) {
$_SESSION['utm_source'] = $_REQUEST['utm_source'];
}
How to get the value in PHP:
if (!empty($_SESSION['utm_source'])) {
...
}
How to get the value in templates or HTML blocks with SMARTY support:
{if $smarty.session.utm_source}
...
{/if}
OK. You can store it in the session. PHP code:
if (!empty($_REQUEST['utm_source'])) {
$_SESSION['utm_source'] = $_REQUEST['utm_source'];
}
Where do I set it , Which file.
Suggest you add the code Ecom provided in the 1st chunk in app/addons/my_changes/controllers/frontend/init.pre.php
Suggest you add the code Ecom provided in the 1st chunk in app/addons/my_changes/controllers/frontend/init.pre.php
Thanks tbirn,
just one query why in init.pre.php cant it be init.post.php as well ? The reason I am asking this is that I have a addon called referrer info which gets referrer info as below but does it in init.post.php so I was thinking if I can add the same code in this file itself.
if(!empty($_SERVER['HTTP_REFERER'])) {
$parse = parse_url($_SERVER['HTTP_REFERER']);
if (Registry::get('config.http_host') != $parse['host']) {
$parse['query'] = (!empty($parse['query'])) ? $parse['query'] : '';
fn_set_cookie('ref_ip', gethostbyname($parse['host']));
fn_set_cookie('ref_domain', $parse['scheme'] . '://' . $parse['host']);
fn_set_cookie('ref_query', $parse['path'] . '?' . $parse['query']);
}
}
You can add it to the init.post.php. I suggested init.pre.php so you would catch it before most other processing on the system. Some times addons or other controllers rewrite the query string and you might lose it. If you want to delay it until the 'post' processing, then that's probably fine in this case.
The difference is that the 'pre' processing sets it before any of the other controllers/addons in the system are initialized. In the 'post' version it is afterward. But both still occur during the 'init' phase of the page load.
You can add it to the init.post.php. I suggested init.pre.php so you would catch it before most other processing on the system. Some times addons or other controllers rewrite the query string and you might lose it. If you want to delay it until the 'post' processing, then that's probably fine in this case.
The difference is that the 'pre' processing sets it before any of the other controllers/addons in the system are initialized. In the 'post' version it is afterward. But both still occur during the 'init' phase of the page load.
Thanks for the clarification, makes sense.
Also can we reset the $_SESSION['utm_source'] once the checkout i complete.
Like in the checkout.post.tpl if we write something like
{if $smarty.session.utm_source eq 'abcsource'}
$smarty.session.utm_source = ''
{/if}
Will it reset the session utm_source param?
Like in the checkout.post.tpl if we write something like
{if $smarty.session.utm_source eq 'abcsource'}
$smarty.session.utm_source = ''
{/if}
Will it reset the session utm_source param?
It is better to do it in the init.post.php file:
if (Registry::get('runtime.controller') == 'checkout' && Registry::get('runtime.mode') == 'complete' && !empty($_SESSION['utm_source'])) {
unset($_SESSION['utm_source']);
}
It is better to do it in the init.post.php file:
if (Registry::get('runtime.controller') == 'checkout' && Registry::get('runtime.mode') == 'complete' && !empty($_SESSION['utm_source'])) {
unset($_SESSION['utm_source']);
}
Will the file be init.post.php or should it be checkout.post.php ?
Will the file be init.post.php or should it be checkout.post.php ?
Actually it is up to you. With the provided code it will work in both cases