Google Analytics Variant Testing different CSS files - php

In my ecommerce store I have a "checkout process" that consist of 3/4 pages. I want to run an experiment on this (A/B testing) with Google Analytics. Specifically; I want to try a different stylesheet that hides a couple of elements (display:none;) to make the checkout more simplistic.
Basically I want Google Analytics to use global.css for the "original" page and global_b.css for the "variant".
Making different variants for every page is not the ideal solution. In the documentation there is a soluton but I don't really understand how to implement this: https://developers.google.com/analytics/solutions/experiments-client-side#implement-choose.
How do I modify the given script to only work for the pages I want to test (e.g. checkout_1.php, checkout_2.php, checkout_3.php) and switch between the two stylesheets?

The way I've approached this is to
1) Add an additional CSS class definition to global.css :
.ga-experiment-no-display{
display:none;
}
2) Add an additional html class to each element that can be hidden on checkout_1|2|3.php, such as '.ga-experiment'
3) The Checkout pages each loads the GA experiment code and includes a JS file (checkout.js). This has code that:
var chosenVariation = cxApi.chooseVariation();
if (chosenVariation > 0){
//Search for all elements with $('.ga-experiment')
//Add the class '.ga-experiment-no-display'
}

Related

How to modify magento front end pages

I am trying to create a module which has both frontend and backend functionality. Like I need to ask for the city in the home page when the store loads. And all the available cities are entered/managed in backend admin panel.
Before I used to write for only backend things, frontend seems little confusing.
There is a design folder which is completely for theme development.
All the example are little different(https://www.mageplaza.com/magento-2-module-development/,http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/]2), they have routes.xml, where route_id, and all are defined, but here I don't need any extra route. Need some additional tweaks in frontend pages.
I created module V_name/M_name/adminhtml/block controllers etc view ...
Guide me how to create a module, which has both front end and backend connection, cities should be entered in admin, they should show on the frontend homepage.
For now, I only managed to edit home page content CMS page by adding some HTML which shows a popup with a dropdown for cities when the page loads.
Since you already have the back-end figured out I will focus on front-end. Also, since all you need to do is populate a list that you already have created this should be easy. I did something like this before and I found it easier to just use JSON to query a list of, in your case the cities, and populate the drop down. I don't believe this is the most 'MVP/proper' way to go, but it is less work then the other ways. (At least for me it is. I always prefer the JavaScript option since it allows for easy future page customization.)
To use the JSON method you need to create a Block with a method like the one below. You will see that you will also have to create a Resource Model (I'm not going to go over creating the Resource Model or the details of Blocks since there are much better resources than me already online that will go into every single detail you need.). Once this is complete you can access the data straight from the .phtml page in an easy to use JSON array.
First you need to make sure you are now structuring your Modules properly. The new Block below should be in a structure like this...
app/code/<VENDOR>/<MODULE>/Block/Wrapper.php (or whatever you name it)
The admin Blocks should be in the structure below, which it sounds like you are already know how to do.
app/code/<VENDOR>/<MODULE>/Block/Adminhtml
Create your Block and add a method to create a JOSN array like below...
public function getCityList()
{
$city_array = array();
/** #var \<VENDOR>\<MODULE>\Model\ResourceModel\City\Collection $collection */
$collection = $this->_cityCollectionFactory->create();
$collection->addFieldToFilter('active','1')->addFieldToSelect(['city_id', 'city']);
$collection->getSelect()->order(array('city ASC', 'city_id ASC'));
$count = 0;
foreach ($collection as $model)
{
$city_array["$count"] = $model->getData();
$count++;
}
return \Zend_Json::encode($city_array);
}
FYI... The foreach loop in the code above is weird and uses $count because I needed to do some tricky things to get something to work.
Then you can create the Block in your .phtml file to access the data via javascript.
<?php
$block_obj = $block->getLayout()->createBlock('<VENDOR>\<MODULE>\Block\Wrapper');
?>
<script type="text/javascript">
window.citylistJson = <?php echo $block_obj->getCityList() ?>;
</script>

Sticky navigation script wont attach class to div

I am currently editting a wordpress theme and creating a navigation bar half way the page that sticks to the top of the screen once the user scrolls past a certain point. Ive done this before and it worked fine. I copied that exact code and changed the #divs and classes to the correct ones. This is the code i use:
$(window).scroll(function () {
console.log($(window).scrollTop())
if ($(window).scrollTop() > 10) {
$("#stickbar-steps").addClass("after-scroll-wrap");
}
if ($(window).scrollTop() < 9) {
$("#stickbar-steps").removeClass("after-scroll-wrap");
}
});
However it doesn't seem to add the class to the #div.When i manually add the class to the #div it works fine but it just wont load the script or wont attach the class. Usually when i reach that point in my previous working one, i could see it getting attached using inspect element, but im not seeing it in this one.
The wordpress theme has a seperate page in the admin section which allows me to paste in JS code. I've tried to add it there and i've tried to paste a copy among the other jquery files in the functions.php. But neither of them seems to work.
Any ideas where to look for? Theme uses jquery ver=1.12.4.
Thanks

Wordpress is page str replace

I have page on wordpress site
http://upsourcemobileservices.com/solutions/
in footer.php I have added the following:
if(is_page('solutions')){
echo str_replace("The MVNi","The MVNi solution was created to ensure that wireless communications are available to the smaller Telco operators.","The MVNi");
}
but it is not changing the contents of the page.
To my knowledge, Syntax is correct str_replace(find,replace,string)
What am I doing wrong?
I also tried using jQuery
function replaceText(jQuery) {
$(".portfolio-thumbnail-content:nth-child(1)").replaceWith("<div class='portfolio-thumbnail-content'>The MVNi solution was created to ensure that wireless communications are available to the smaller Telco operators.</div>");
$(".portfolio-thumbnail-content:nth-child(2)").replaceWith("<div class='portfolio-thumbnail-content'>Primarily aimed at existing Telcos our MVNc solution integrates your existing platforms with ours through the use of APIs.</div>");
$(".portfolio-thumbnail-content:nth-child(3)").replaceWith("<div class='portfolio-thumbnail-content'>UMS turnkey solution is a complete MVNO in a box.</div>");
$(".portfolio-thumbnail-content:nth-child(4)").replaceWith("<div class='portfolio-thumbnail-content'>This solution allows you to select the products and services that you want and integrate them with your existing platforms.</div>");
}
$(document).ready(replaceText);
but can't seem to get that working either, here's a fiddle http://jsfiddle.net/vzy9R/3/
I used jQuery eq() selector to find elements containing class portfolio-thumbnail-content and then using .replaceWith() I was able to load my teaser content (excerpts) ending with complete sentences.
For example, the About page:
(function ($) {
$(".portfolio-thumbnail-content:eq(0)").replaceWith("<div class='portfolio-thumbail-content'>This is my new complete sentence teaser for Team.</div>");
$(".portfolio-thumbnail-content:eq(1)").replaceWith("<div class='portfolio-thumbail-content'>This is my new complete sentence teaser for Partners.</div>");
})(jQuery);
Then I had to figure out a way to implement those scripts into the body of those pages, so I wrote my own shortcodes and added them to functions.php
//[replaceTextAbout]
function replaceTextAbout_func( $atts ){
return "<script src='/javascript/custom/replaceText-About.js'></script>";
}
add_shortcode( 'replaceTextAbout', 'replaceTextAbout_func' );

Adding a javascript to all pages in OpenCart

I'm new to OpenCart and don't have any experience with PHP, so I have a question. I want to add the following JavaScript to hide the url bar on mobile browsers
// When ready...
window.addEventListener("load",function() {
// Set a timeout...
setTimeout(function(){
// Hide the address bar!
window.scrollTo(0, 1);
}, 0);
});
However, I can't find a way to insert this so this code will be executed on all pages in OpenCart. Where should I put this code?
save your script to a file, say 'catalog/view/javascript/myscript.js'
Then add
$this->document->addScript('catalog/view/javascript/myscript.js');
to the catalog/controller/common/header.php some place before this line:
$this->data['scripts'] = $this->document->getScripts();
You could also just place your script inline into catalog/view/theme/{theme name}/template/common/header.tpl using normal html markup.
Looking at the theme documentation, I believe you want to edit the following file:
catalog/view/theme/{your-theme}/template/common/header.tpl
These templates (header, footer, etc) should appear on all pages.
You don't need to go through this trouble especially if you don't have access to FTP. All you need to do is just go to admin panel > design > theme editor > and choose the respective parts to insert the codes. If is footer then just choose footer.twig.
After adding the codes, click Save and you will see the changes immediately. If add the codes directly to the file on FTP, it won't work.
Tried and tested on OpenCart 3
To add a script like that, just go to admin panel > design > theme editor > select "common" > footer and in the end of the file (after </html>) add the script.
If you want script to all your pages of the OC just add it before footer tag in footer.tpl or footer.twig

Accessing $_SESSION from outside of Joomla

I used Joomla 1.5 for a site that I developed for a gaming company. Part of the site consists of a character generater for the game that they developed. The issue is that users want to be able to print the character sheets off, without having the Joomla template surrounding it.
As for the specifics, I have the directphp extension installed, and the entire generator is written in PHP (with a little JavaScript to handle things that PHP can't). As the generator spans several dozen page calls, it made sense to store everything in $_SESSION. All of this works correctly. In an attempt to make the final sheet printer friendly, I tried redirecting the user to a page outside of Joomla (though on the same server, and even within the same folder) but I cannot access the $_SESSION data from this new page.
I have seen several posts (a few on this site) that point to loading the Joomla Framework and accessing it that way, which I have tried, but the data that I was looking for does not appear to be contained there. Has anyone come across this problem before, or know how to get to that data?
You are making this WAY harder than it needs to be. You don't have to write any additional code to accomplish what you are trying to do. In order to print our the component output without all of the Joomla template, you just append ?tmpl=component to your URLs and Joomla will display only to component output without any of the template. If you want to give it a custom stylesheet or anything special, you can also add in a template override by adding a file named component.php in your template folder.
In order to control the CSS per page, you can add Page Class Suffixes in the menu items. Then add this code to index.php so you can use them.
Somewhere in the head add this:
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass = "";
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = trim($params->get( 'pageclass_sfx' ));
endif;
Replace your body tag with this:
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">
Any page that you do not specific a Page Class Suffix for will use default as the body ID, any that you do will use what ever you specify.

Categories