Error: Before calling any methods on the analytics, you first need to call the 'init' function with appId and apiKey parameters

Hi, I have a search bar that uses autocomplete via Algolias default Shopify integration.

It works with personalisation and on desktop is fine and works as intended (on suggestion click, it will go directly to either the PDP or collection page), but on mobile AND Safari / IOS , it just fills out the search bar and the user has to press the enter / search key.

This is also causing drops in click through rates.

Ive followed the guides to adding conversions and personalisation which all works fine on desktop, maybe i’m missing a step for mobile / ios / safari? Has anyone else had the same issue?

In algolia_autocomplete.js.liquid

   function getCookie(cookieName) {
    let cookie = {};
    document.cookie.split(';').forEach(function(el) {
      let [key,value] = el.split('=');
      cookie[key.trim()] = value;
    })
    return cookie[cookieName];
  }

 var searchOpts = {
      clickAnalytics: true,
      enablePersonalization: true,
      userToken: getCookie('_ALGOLIA'),
      hitsPerPage: params.hitsPerPage,
      highlightPreTag: '<span class="aa-highlight">',
      highlightPostTag: '</span>',
    };

// Event listeners
  autocompleteInstance.on('autocomplete:selected', function(obj, datum, name) {
    if (algolia.config.analytics_enabled) {
      var clickData = {
        index: datum._index,
        eventName: 'click',
        queryID: datum._queryID,
        objectIDs: [datum.objectID],
        positions: [datum._position],
        userToken: getCookie('_ALGOLIA'),
        appId: 'MB13IY345Y',
        apiKey: '5a50c24f3e92ae83f808068a942a1f3b'
      };

      // Send the click event
      //aa.clickedObjectIDsAfterSearch(clickData);
      window.aa('clickedObjectIDsAfterSearch', clickData);
      /**
       * Uncomment the following function call to start storing data in
       * local storage for conversion tracking
       */
      algolia.saveForConversionTracking(clickData);
    }
    switch (name) {
      case 'products': {
        var addVariantId =
          !algolia.config.show_products && datum.objectID !== datum.id;
        window.location.href =
          '/' +
          name +
          '/' +
          datum.handle +
          (addVariantId ? '?variant=' + datum.objectID : '');
        break;
      }
      case 'articles':
        window.location.href =
          '/blogs/' + datum.blog.handle + '/' + datum.handle;
        break;
      default:
        window.location.href = '/' + name + '/' + datum.handle;
    }
  });

^^^ triggers the click through on desktop by building the suggestions url

  // Hack to handle buggy onclick event on iOS
  autocompleteInstance.each(function(i) {
    var data = autocompleteInstance.eq(i).data('aaAutocomplete');
    var suggestionClass =
      '.' +
      data.dropdown.cssClasses.prefix +
      '-' +
      data.dropdown.cssClasses.suggestion;
    var onSuggestionClick = data.dropdown._onSuggestionClick.bind(
      data.dropdown
    );

    var touchmoved;
    data.dropdown.$menu
      .on('touchend', suggestionClass, function(e) {
        if (touchmoved === false) {
          e.preventDefault();
          e.stopPropagation();
          onSuggestionClick.apply(this, arguments);
        }
      })
      .on('touchmove', function() {
        touchmoved = true;
      })
      .on('touchstart', function() {
        touchmoved = false;
      });
  });

^^^ Could be something to do with this snippet?