Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying to create a php file which basically is a mobile shortcode message. Since any output on the page automatically gets shown in the mobile SMS, I cannot use any html on the page. But I am having a problem in executing some google analytics javascript code before the text is outputted on the page. I create an external file and wrote the javascript there and tried to execute the file via curl, but curl does not execute the javascript. So my code for the main file SMSapi.php is something like this:

<?php
    if(isset($_GET['mobile'])){
        $number = $_GET['mobile'];
    }

    if(isset($_GET['text'])){
        $data = $_GET['text'];
    }

    $brand = "mybrand";
    $event = "MyEvent";
    $deal = "MyDeal";

    $url = "http://myurl.com/sms.php";

    $post_string = "brand={$brand}&event={$event}&deal={$deal}";

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $success = curl_exec($ch);

    curl_close($ch);

    echo "Your discount code is XYZ123";
?>

The sms.php code is as follows:

<?php 

if(isset($_POST) && !empty($_POST['event']) && !empty($_POST['brand']) && !empty($_POST['deal'])):
    $event = urldecode($_POST['event']);
    $brand = urldecode($_POST['brand']);
    $deal = urldecode($_POST['deal']);
?>
<html>
    <head>
    <script type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-MyNum']);
        _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
    _gaq.push(['_trackEvent', '<?php echo $event; ?>', '<?php echo $brand; ?>', '<?php echo $deal; ?>']);
</script>
</head>
</html>
<?php endif ?>

the main SMS api file makes a curl request to the sms.php file and while the file gets executed, the html and javascript gets returned back as text without any execution happening there. And hence the javascript shows up in the SMS.

Is there a way to implement a external url and all the javascripts in it there and there via php?

share|improve this question
    
are you aware of ob_start() and other output buffering functions? They might be of help for you. –  J0HN Oct 5 '11 at 8:22
    
output buffer can hide html which is useful, but that also does not implement html and javascript which is the problem as i need the javascript to make an entry in google analytics –  Shiva Oct 5 '11 at 9:17

2 Answers 2

I am presuming this is a script that runs on a call from an SMS provider, and you are tying to log this event in Google Analytics.

Firstly, I would have thought this would more easily be accomplished by logging to a database, rather than to Analytics. As the javascript is running server side, you won't get any extra information other that the event.

If you really do need to run the Google code then you need to try searching on "server side javascript google analytics". The solution is going to be highly dependant on what server platform you are running and what is/can be installed.

One interesting link though which may work for your PHP is:

http://code.google.com/p/serversidegoogleanalytics/

but I haven't used it so don't know how well this works.

share|improve this answer

This link might be useful for you: http://curl.haxx.se/docs/faq.html#Does_curl_support_Javascript_or

share|improve this answer
    
Do you know of any alternative solutions that could help implement all the javascripts in an external file or url?? Curl certainly cannot do what i seek, but it would be great if you can let me know of any alternatives you might have come accross. –  Shiva Oct 5 '11 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.