Difference between revisions of "CR.cookie"
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | <span class="code">__CR. | + | <span class="code">__CR.cookie</span> is a function which takes two parameters and gets or sets [[Cookie|cookie values]] within the current page's domain. (Or optionally a specific domain.) |
== Setting a cookie value == | == Setting a cookie value == | ||
Line 23: | Line 23: | ||
On the form page: | On the form page: | ||
− | + | <script> | |
− | + | window._crq = window._crq || []; | |
− | + | window._crq.push(function (cr) { | |
− | + | cr.jquery(function ($) { | |
− | + | var | |
− | + | save_email = function () { | |
− | + | cr.cookie("cr-email", $(this).val()); | |
− | + | }; | |
− | + | $('input[type=email],input.email').on("change", save_email).each(save_email); | |
− | + | }); | |
− | + | }); | |
− | + | </script> | |
+ | |||
+ | '''The above code will only be functional on a page which contains a [[ConversionRuler Tracking Snippet]].''' | ||
On the form thank you page: | On the form thank you page: | ||
− | + | <script> | |
− | + | window._crq = window._crq || []; | |
− | + | window._crq.push(function (cr) { | |
− | + | cr.track('thank-you', null, cr.cookie('cr-email')); | |
− | + | }); | |
− | + | </script> | |
+ | |||
+ | '''The above code will only be functional on a page which contains a [[ConversionRuler Tracking Snippet]].''' | ||
Note that the above should be customized for the type of elements targeted by [[jQuery]] as well as the action code names, depending on your installation. | Note that the above should be customized for the type of elements targeted by [[jQuery]] as well as the action code names, depending on your installation. | ||
Line 53: | Line 57: | ||
For example, if your site has the following domains: | For example, if your site has the following domains: | ||
− | * http://example.com/ | + | * <nowiki>http://example.com/</nowiki> |
− | * http://www.example.com/ | + | * <nowiki>http://www.example.com/</nowiki> |
− | * http://blog.example.com/ | + | * <nowiki>http://blog.example.com/</nowiki> |
− | * http://store.example.com/ | + | * <nowiki>http://store.example.com/</nowiki> |
− | You would set the domain as follows: | + | You would set the domain as follows so that cookies would be shared between your domains: |
− | + | <script> | |
− | + | window._crq = window._crq || []; | |
− | + | window._crq.push(function (cr) { | |
− | + | cr.domain = ".example.com"; | |
− | + | // Do cookie set/get calls | |
− | + | }); | |
− | + | </script> | |
== See also == | == See also == | ||
* [[:Category:ConversionRuler JavaScript Functions|JavaScript Functions]] | * [[:Category:ConversionRuler JavaScript Functions|JavaScript Functions]] | ||
− | * [[__CR. | + | * [[__CR.data]] |
+ | * [[__CR.query]] | ||
* [[__CR.jquery]] | * [[__CR.jquery]] | ||
* [[Tracking Queue]] | * [[Tracking Queue]] |
Latest revision as of 16:48, 14 November 2018
__CR.cookie is a function which takes two parameters and gets or sets cookie values within the current page's domain. (Or optionally a specific domain.)
Contents
Setting a cookie value
__CR.cookie = function (name, value) { ... }
The name is the exact name of the query string variable to store. The value is a simple string or numeric value to store. Currently, no encoding of non-scalar structures (including null or false) is supported (e.g. objects, arrays) so you must encode and decode your content prior to passing into the __CR.cookie call. Note as well that expressions which evaluate to false are not allowed as well.
Getting a cookie value
__CR.cookie = function (name) { ... }
The name is the exact name of the query string variable to retrieve.
Deleting a cookie value
__CR.delete_cookie(name)
Deletes the cookie with name name if it exists.
Example usage - saving and recording an email address on two pages
On the form page:
<script> window._crq = window._crq || []; window._crq.push(function (cr) { cr.jquery(function ($) { var save_email = function () { cr.cookie("cr-email", $(this).val()); }; $('input[type=email],input.email').on("change", save_email).each(save_email); }); }); </script>
The above code will only be functional on a page which contains a ConversionRuler Tracking Snippet.
On the form thank you page:
<script> window._crq = window._crq || []; window._crq.push(function (cr) { cr.track('thank-you', null, cr.cookie('cr-email')); }); </script>
The above code will only be functional on a page which contains a ConversionRuler Tracking Snippet.
Note that the above should be customized for the type of elements targeted by jQuery as well as the action code names, depending on your installation.
Using a custom domain
You can set the ConversionRuler JavaScript Global member domain to use an alternate domain name (e.g. a shared top-level domain name, for example) prior to using these calls.
For example, if your site has the following domains:
- http://example.com/
- http://www.example.com/
- http://blog.example.com/
- http://store.example.com/
You would set the domain as follows so that cookies would be shared between your domains:
<script> window._crq = window._crq || []; window._crq.push(function (cr) { cr.domain = ".example.com"; // Do cookie set/get calls }); </script>